a042. 平面圓形切割
a0 = 1 a1 = 2 a2 = 4 a3 = 8 a4 = 14 a5 = 22an - an-1 = 2 * (n - 1), if n > 1
=> an = 2 * (n - 1) + an-1
寫成遞迴就OK了,也可以寫成公式
an = n2 - n + 2, if n > 0
程式碼如下:
/* a042. 平面圓形切割 * * 2020/3/3 */ import java.util.Scanner; public class Pa042{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ System.out.println(circleCut(scanner.nextInt())); } } public static int circleCut(int n){ return n == 1? 2: 2 * (n - 1) + circleCut(n - 1); } }
沒有留言:
張貼留言