标签:
验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。
例如:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
1 import java.util.Scanner; 2 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner scanner=new Scanner(System.in); 7 int n=scanner.nextInt(); 8 System.out.println(getStr(n)); 9 scanner.close(); 10 } 11 12 public static String getStr(int n) 13 { 14 int mid=n*n; 15 int tar=n*n*n; 16 StringBuilder stringBuilder=new StringBuilder(); 17 int sum=0,dis=mid%2==0?-1:0; 18 while(dis<=tar) 19 { 20 if(dis==0) 21 sum+=mid; 22 if(sum==tar) 23 break; 24 sum+=2*mid; 25 dis+=2; 26 } 27 for(int i=mid-dis;i<=mid+dis;i+=2) 28 { 29 if(i==mid+dis) 30 stringBuilder.append(i); 31 else 32 stringBuilder.append(i+"+"); 33 } 34 return stringBuilder.toString(); 35 } 36 }
标签:
原文地址:http://www.cnblogs.com/maydow/p/4782404.html