题目描述:
N!
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 262144/262144K (Java/Other)
Total Submission(s) : 68 Accepted Submission(s) : 20
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
这道题要用数组来储存最终的结果。
注意一个很小的细节(俺在那里测了老半天,伤!),就是0的阶乘为1;
下面附上自己写的代码:
1 #include <stdio.h> 2 #include <time.h> 3 #include <string.h> 4 5 6 #define mod 10000 7 8 int main() { 9 short int rem[10000],i,index,j; 10 int n,jw; 11 // clock_t start,finish; 12 // start = clock(); 13 // double total = 1; 14 while(~scanf("%d", &n)) { 15 memset(rem, 0, sizeof(rem)); 16 rem[1] = 1; 17 index = 2; 18 jw = 0; 19 // if (n == 0) 20 // printf("0"); 21 for (i = 1; i <= n;i++) { 22 for (j=1;j<index;j++) { 23 int tem = rem[j]*i+jw; 24 rem[j] = tem % mod; 25 jw = tem / mod; 26 } 27 if (jw) { 28 rem[index] = jw; 29 jw = 0; 30 index++; 31 } 32 } 33 for (i=index-1;i>=1;i--) { 34 if (i == index-1) 35 printf("%d", rem[i]); 36 else 37 printf("%04d", rem[i]); 38 } 39 puts(""); 40 } 41 // finish = clock(); 42 // printf(" 程序用时为:%lf", (double)(finish-start)/CLOCKS_PER_SEC); 43 return 0; 44 }