Java 阶乘和

用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=5*4*3*2*1。

输入正整数N,输出计算结果S。
输入
一个正整数N。
输出
计算结果S。
样例输入
5

样例输出
153

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n;
        BigInteger sum = new BigInteger("0");
        BigInteger p = new BigInteger("1");
        n=scan.nextInt();
        for(int i=1;i<=n;i++)
        {
             BigInteger temp = new BigInteger(i+"");
             p = p.multiply(temp);
             sum = sum.add(p);
        }
        System.out.println(sum);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41611106/article/details/80315469
今日推荐