题目描述
用高精度计算出 S = 1! + 2! + 3! + \cdots + n!S=1!+2!+3!+⋯+n!(n \le 50n≤50)。
其中“!”表示阶乘,例如:5! = 5 \times 4 \times 3 \times 2 \times 15!=5×4×3×2×1。
输入格式
一个正整数 nn。
输出格式
一个正整数 SS,表示计算结果。
方法一:不讲武德法
java 里有个封装类叫 BigInteger,支持大数操作....
private static final BigInteger[] INTEGERS = new BigInteger[51];
static {
INTEGERS[0] = new BigInteger("1");
}
public static void main(String[] args) {
BigInteger result = new BigInteger("0");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1; i <= n; ++ i) {
INTEGERS[i] = INTEGERS[i - 1].multiply(new BigInteger(String.valueOf(i)));
result = result.add(INTEGERS[i]);
}
System.out.println(result);
}
方法二:高精度
用 int 数组储存大数,并在更新时从前向后逐位遍历并进行进位操作。
代码略。(别打我