[NOIP1998 普及组] 阶乘之和
题目描述
用高精度计算出 S = 1! + 2! + 3! + ... + n!(n <= 50)。
其中 !
表示阶乘,定义为 n!=n * (n-1) * (n-2) * ... * 1。例如,5! = 5 * 4 * 3 * 2 * 1=120。
输入格式
一个正整数 n。
输出格式
一个正整数 S,表示计算结果。
样例 #1
样例输入 #1
3
样例输出 #1
9
提示
提交程序
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 bigint = BigInteger.valueOf(0);
for (int i = 1; i <= n; i++) {
BigInteger b1 = BigInteger.valueOf(1);
for (int j = 1; j <= i; j++) {
BigInteger b2 = BigInteger.valueOf(j);
//multiply:返回相乘的值
b1 = b1.multiply(b2);
}
//add:返回相加的值
bigint = bigint.add(b1);
}
System.out.println(bigint);
}
}