题目描述
用高精度计算出S=1!+2!+3!+…+n!(n≤50)S=1!+2!+3!+…+n! (n≤50)S=1!+2!+3!+…+n!(n≤50)
其中“!”表示阶乘,例如:5!=5×4×3×2×15!=5 \times 4 \times 3 \times 2 \times 15!=5×4×3×2×1。
输入格式
一个正整数NNN。
输出格式
一个正整数SSS,表示计算结果。
输入输出样例
输入
3
输出
9
import java.math.BigInteger;
import java.util.Scanner;
public class P1009 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BigInteger total = new BigInteger("0");
for (int i=1;i<=n;i++){
total = total.add(f(i));
}
System.out.print(total);
}
public static BigInteger f(int a){
BigInteger sum = new BigInteger("1");
for (int i=a;i>=1;i--){
sum = sum.multiply(BigInteger.valueOf(i));
}
return sum;
}
}