题目描述:
验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。m属于[1,100],超出范围则报错。
例如:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
import java.util.Scanner;
public class NicoChester {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
checkNicoChester(num);
}
private static void checkNicoChester(int num) {
int []result = new int[num];
int len = result.length-1;
if(num%2 != 0){
for (int i = 0; i < len; i++) {
result[i] = num*num-2*(result.length/2-i);
System.out.print(result[i]+"+");
}
System.out.print(num*num-2*(result.length/2-len));
}
else{
for (int i = 0; i < len; i++) {
result[i] = num*num-num+1+2*i;
System.out.print(result[i]+"+");
}
System.out.print(num*num-num+1+2*len);
}
}
}