首页 > 代码库 > 1042 N!

1042 N!

 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 using namespace std; 5 const int MAX=5000; 6 int array[MAX+5]; 7  8 int main() 9 {10     int n;int i,j;11     while(cin>>n)12     {13         14         memset(array,0,sizeof(array));15         array[0]=1;16        17       18       19          for ( i=2 ; i<=n; i++)20         {21             int c=0;22             for ( j=0; j<MAX; j++)23             {24                 int s=array[j]*i+c;25                 array[j]=s%100000;26                 c=s/100000;27             }28         }               29         30         31         for ( j=MAX-1; j>=0; j--)32             if (array[j])33                 break; 34         printf("%d",array[j]);35         for ( i=j-1; i>=0; i--)36             printf("%05d",array[i]);37         printf("\n");38     }39 return 0;    40 }

 

 

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58167    Accepted Submission(s): 16520


Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
 

Input
One N in one line, process to the end of file.
 

Output
For each N, output N! in one line.
 

Sample Input
1 2 3
 

Sample Output
1 2 6

对这题已经彻底无语了。已经断断续续一个多月了。

2点注意:

  1 数组大小 8000(7000不行)

  2 一个数组元素存一个五位数。输出时。不够五位数的前面加零。想想为什么。 

1042 N!