题目描述
任何一个自然数m的立方均可写成m个连续奇数之和。例如:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
现在让你求111^3中的连续奇数和,若有多组数据,输出开头最小的那组数据的第一个数据!
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 111;
int s = n * n * n;
int temp;
for (int i = 1; i <= s; i += 2) {
temp = 0;
for (int j = i; j <= s; j += 2) {
temp += j;
if (temp == s) {
sop(i, j);
} else if (temp > s) {
break;
}
}
}
}
private static void sop(int i, int j) {
// TODO Auto-generated method stub
if (i == j) {
return;
}
for (int k = i; k <= j; k += 2) {
System.out.println(k + " ");
}
System.out.println("--------");
}
} |
|