NYOJ 28 大数阶乘

大数阶乘

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述
我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
 
输入
输入一个整数m(0<m<=5000)
输出
输出m的阶乘,并在输出结束之后输入一个换行符
样例输入
50
样例输出
30414093201713378043612608166064768844377641568960512000000000000

模拟题:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define N 1000010
 4 int a[N];
 5 int imitation(int n)
 6 {
 7     int i;
 8     for(i=0;a[i]>=0;i++)
 9     {
10         a[i]*=n;
11     }
12     for(i=0;a[i]>=0;i++)
13     {
14         if(a[i]>=10)
15         {
16             if(a[i+1]<0)
17                 a[i+1]=0;
18             a[i+1]+=a[i]/10;
19             a[i]%=10;
20         }
21 
22     }
23     return i+1;
24 
25 }
26 int main()
27 {
28     int i,m,flag;
29     memset(a,-1,N*sizeof(int));
30     scanf("%d",&m);
31     a[0]=1;
32     for(i=2;i<=m;i++)
33     {
34         flag=imitation(i);
35     }
36     for(i=flag;i>=0;i--)
37     {
38         if(a[i]>=0)
39             printf("%d",a[i]);
40     }
41     printf("
");
42     return 0;
43 }
 1  
 2 #include<iostream>
 3 #include<iomanip>
 4 using namespace std;
 5 //存储20000以内的阶乘
 6 int a[15470];
 7 int main()
 8 {
 9     //freopen("1.txt","r",stdin);
10     //freopen("2.txt","w",stdout);
11     int n;
12     cin>>n;
13     a[1]=1;
14     a[0]=1;
15     int up;
16     for(int i=2;i<=n;++i)
17     {
18         up=0;
19         for(int j=1;j<=a[0];++j)   //各个位相乘
20         {
21             a[j] *=i;
22             a[j] +=up;
23             up=a[j]/100000;
24             a[j] %=100000;
25         }
26         if(up!=0)
27         {
28             a[0]++;
29             a[a[0]]=up;
30         }
31     }
32     if(a[0]==1) cout<<a[1];
33         else
34         {
35             cout<<a[a[0]];
36             for (int i=a[0]-1;i>0;i--)
37             {
38                 cout<<setfill('0')<<setw(5)<<a[i];
39             }
40         }
41 }        
原文地址:https://www.cnblogs.com/ljwTiey/p/4303097.html