首页 > 代码库 > N!阶乘

N!阶乘

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e4;
 4 void factorial(int n){
 5     int a[N];
 6     a[0] = 1;
 7     int res = 0;
 8     for(int i = 1; i <= n; i ++){
 9         int flag = 0;
10         for(int j = 0; j <= res; j ++){
11             a[j] = a[j]*i + flag;
12             flag = a[j]/10000;
13             a[j]%=10000;
14         }
15         if(flag > 0){
16             a[++res] = flag;
17         }
18     }
19     cout << a[res];
20     for(int i = res-1; i >= 0; i--){
21         cout << setw(4) << setfill(0) << a[i];
22     }
23     cout << endl;
24 }
25 int main(){
26     int n;
27     while(~scanf("%d",&n)){
28         factorial(n);
29     }
30     return 0;
31 }

 

N!阶乘