public static void main(String[] args) {
// 1*1+2*2+3*3+n*n
int n = 40;
// 1 5 14 30 55
// 1 2 3 4 5
// 方式一
int c = 0;
for (int i = 1; i <= n; i++) {
int c1 = count(i);
c = (c1 + i * i);
}
System.out.println(c);
// 方式二
c = 0;
c = n * (n + 1) * (2 * n + 1) / 6;
System.out.println(c);
}
public static int count(int n) {
int c = 0;
for (int i = 1; i < n; i++) {
c += i * i;
}
return c;
}
// 1*1+2*2+3*3+n*n
int n = 40;
// 1 5 14 30 55
// 1 2 3 4 5
// 方式一
int c = 0;
for (int i = 1; i <= n; i++) {
int c1 = count(i);
c = (c1 + i * i);
}
System.out.println(c);
// 方式二
c = 0;
c = n * (n + 1) * (2 * n + 1) / 6;
System.out.println(c);
}
public static int count(int n) {
int c = 0;
for (int i = 1; i < n; i++) {
c += i * i;
}
return c;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。