题目描述
刚开始,直接上手,来一个表情吧
做了提交,还是有些案列没有跑出来,我哭了。。。。。
经过研究与查资料,终于解决了。哈哈哈哈哈哈
知识点:
(1)java的bigInteger实现了高精度阶乘的计算。
BigInteger sum=BigInteger.valueOf(0)------》赋予sum的值为0
(2) BigInteger b=BigInteger.valueOf(2), a=a.multiply(b)-------》计算2的阶乘
(3)BigInteger sum=BigInteger.valueOf(0), sum=sum.add(2)-----》结果为0+2
上代码
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//从键盘获取输入的数
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
BigInteger sum=BigInteger.valueOf(0);
for(int i=1;i<=n;i++){//n个数
BigInteger a=BigInteger.valueOf(1);
for(int j=1;j<=i;j++){//计算每一个数的阶乘
BigInteger b=BigInteger.valueOf(j);
a=a.multiply(b);
}
sum=sum.add(a);
}
System.out.println(sum);
}
}