N的阶乘大数据高精度写法

时间:2022-10-26 03:36:44
题目描述:

 输入一个正整数N,输出N的阶乘。

输入:

正整数N(0<=N<=1000)

输出:

 输入可能包括多组数据,对于每一组输入数据,输出N的阶乘

样例输入:
4
5
15
样例输出:
24
120
1307674368000
# include <iostream>
# define maxn 3000
using namespace std;

int data[maxn];

int main()
{
    int n;
    while (cin >> n)
    {
        int temp,l;
        for (int i = 1;i<=maxn;i++)
            data[i] = 0;
        data[1] = 1;
        l = 1;
        for (int i = 1;i<=n;i++)
        {
            temp = 0;
            for (int j = 1;j<=l;j++)
            {
                data[j] = data[j]*i+temp;
                temp = data[j] / 10;
                data[j] = data[j] % 10;
            }
            while (temp != 0)
            {
                data[++l] = temp % 10;
                temp = temp / 10;
            }
        }
        for (int i = l;i>=1;i--) cout << data[i] ;
        cout <<endl;
    }
    return 0;
}