这道题考察的是找规律
尼科彻斯定理
https://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int start = 1;
/**
* 规律为:开始的奇数=上一个数+从0开始到该数时一直累加2的数
* 1 1 1+0
* 2 3 1+0+2
* 3 7 1+0+2+4
* 4 13 1+0+2+4+6
*/
// 注意累加时是从0开始,所以为i < n
for (int i = 0, j = 0; i < n; i++, j += 2) {
start += j;
}
StringBuilder builder = new StringBuilder();
builder.append(start);
int result = start;
// n的3次方
int pow = (int) Math.pow(n, 3);
while (result != pow) {
// 奇数递增2为下一个奇数
start += 2;
// 每次加下一个奇数
result += start;
builder.append("+").append(start);
}
System.out.println(builder);
}
}