题解 | #等差数列#
等差数列
http://www.nowcoder.com/practice/f792cb014ed0474fb8f53389e7d9c07f
递归求出第n个数组, An = An-1 + 3, A0 =2;没啥技巧。
import java.util.Scanner; public class Main { public static void main(String[] arg) { Scanner scan = new Scanner(System.in); while (scan.hasNextInt()) { int n = scan.nextInt(); int[] inputArr = new int[n]; for (int i = 0; i < n; i++) { inputArr[i] = getItem(i); } int total = 0; for (int i = 0; i < inputArr.length; i++) { total +=inputArr[i]; } System.out.println(total); } } public static int getItem(int i){ if(i==0){ return 2; }else { return getItem(i - 1) + 3; } } }