大数的阶乘

我们以1000为例,为了保存结果,需要先分析1000!有多大。大约为4*10的2567次方,因此可以用一个3000个的元素的数组f保存。为了方便进位,我们让f[0]保存各位,f[1]保存十位。。。。。。在输出时要忽略前导0。4!=24,那么从f[2]~f[1000]都为0需要忽略输出。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=3000;
 7 int f[maxn];
 8 int main()
 9 {
10     int i,j,n;
11     while(cin>>n)
12     {
13         memset(f,0,sizeof(f));
14         f[0]=1;
15         for(i=2;i<=n;i++)
16         {
17             int c=0;
18             for(j=0;j<maxn;j++)
19             {
20                 int s=f[j]*i+c;
21                 f[j]=s%10;
22                 c=s/10;
23             }
24         }
25         for(j=maxn-1;j>=0;j--)
26         if(f[j])break;//忽略前导0
27         for(i=j;i>=0;i--)
28         cout<<f[i];
29         cout<<endl;
30     }
31     return 0;
32 }
原文地址:https://www.cnblogs.com/WHLdbk/p/5745848.html