基础练习 阶乘计算

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 void sepreate(int m, int a[]){
 5     a[0]=0;
 6     int i=1;
 7     while(m){
 8         a[i++]=m%10;
 9         m/=10;
10         a[0]++;
11     } 
12 }
13 
14 int main(){
15     int n;
16     cin>>n;
17     int result[100000]={0}, temp[100000]={0}, a[10000]={0};
18     temp[0]=1;
19     temp[1]=1;
20     
21     for(int i=1; i<=n; i++){
22         sepreate(i, a);
23         result[0]=temp[0]+a[0]+1;
24         
25         for(int j=1; j<=temp[0]; j++){
26             for(int k=1; k<=a[0]; k++){
27                 result[j+k]+=temp[j]*a[k];
28                 result[j+k+1]+=result[j+k]/10;
29                 result[j+k]%=10;
30             }
31         }
32         
33         while(result[result[0]]==0){
34             result[0]--;
35         }
36         int l=result[0];
37         temp[0]=result[0]-1;
38         for(int s=2; s<=l; s++){        
39             temp[s-1]=result[s];
40             result[s]=0;
41         }
42 
43     }
44     
45     if(temp[0]<1)
46     cout<<0;
47     else
48     for(int i=temp[0]; i>0; i--)
49     {
50         cout<<temp[i];
51     }
52     return 0;
53 }