响应党的指示,完全按照党的意思,望采纳。谢谢
public class Test {
public static void main(String[] args) {
for(int i = 1; i <= 20; i++) {
long sum = 0;
long tem = 1;
for(int j = 1; j <=i; j++) {
tem = tem*j;
sum=sum +tem;
if(j < i) {
System.out.print(j + "! + ");
}else System.out.println(j + "!"+" = " + sum);
}
}
}
}
运行结果如下:
1! = 1
1! + 2! = 3
1! + 2! + 3! = 9
1! + 2! + 3! + 4! = 33
1! + 2! + 3! + 4! + 5! = 153
1! + 2! + 3! + 4! + 5! + 6! = 873
1! + 2! + 3! + 4! + 5! + 6! + 7! = 5913
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! = 46233
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! = 409113
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! = 4037913
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! = 43954713
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! = 522956313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! = 6749977113
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! = 93928268313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! = 1401602636313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! = 22324392524313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! = 378011820620313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! = 6780385526348313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! + 19! = 128425485935180313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! + 19! + 20! = 2561327494111820313
public class Sum {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
long sum = 0;
long k = 1;
for (int j = 1; j <= i; j++) {
k = k * j;
sum = sum + k;
System.out.print(j + "!");
if (i == j) {
System.out.print("=");
} else {
System.out.print("+");
}
}
System.out.println(sum);
}
}
其中n的值可以随便改成任意整数
public static void main(String[] args) {
String info = "";
long sum_total = 0;
for (int i = 1; i <= 20; i++) {
int sum = 1;
for (int j = 1; j <= i; j++) {
sum = sum * j;
}
sum_total += sum;
info += "+ " + i + "! ";
System.out.println(info.substring(2, info.length()) + "= " + sum_total);
}
}
刚敲好。楼主看下吧。有不懂的可以再问,代码如下:
public class Test {
//计算阶乘
public static long factory(int n) {
long value = 1;
for(int i = 1; i <= n; i++) {
value *= i;
}
return value;
}
public static void main(String[] args) {
long sum = 0;
for(int i = 1; i <= 9; i++) {
//计算累加
sum += factory(i);
//输出格式
for(int j = 1; j <= i; j++) {
System.out.print(j + "!");
if(j != i) {
System.out.print(" + ");
}
}
System.out.println(" = "+sum);
}
}
}
总结:楼上楼下的都是大侠,我在这里学习了