求1!+2!+3!+…+10!的值

  • Post author:
  • Post category:其他


我们把复杂问题简单化,分两部

第一,我们分别求出1,2,3…10的阶乘;

比如4的阶乘 1

2

3*4 ;我们可以通过循环分别把这些阶乘求出来;

第二,我们把10个数字的阶乘相加即可;

public class Test {
public static void main(String[] args) {
    int total=0;
    for(int i=1;i<=10;i++){
        int cTotal=1;
        for(int j=1;j<=i;j++){
            cTotal*=j;
        }
        total+=cTotal;
        System.out.println(i+"!="+cTotal);
    }
    System.out.println("1!+2!+3!+...+10!的阶乘和是:"+total);
}
}

运行输出:

1!=1

2!=2

3!=6

4!=24

5!=120

6!=720

7!=5040

8!=40320

9!=362880

10!=3628800

1!+2!+3!+…+10!的阶乘和是:4037913