HDOJ1042(N!)(大数乘)
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 67802 Accepted Submission(s): 19420
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
/*2015.10.29*/
#include<stdio.h> #include<string.h> int t1[10],t2[40000],t3[40000],n,m; void mul() { int i,j,k,g; k=n; for(i=0;i<m;i++) { k=i; for(j=0;j<n;j++) { t3[k++]+=t1[i]*t2[j]; } } for(i=0;i<k;i++) { if(t3[i]>9)/*t3起转换容器的变量,暂时存放计算结果*/ { g=t3[i]/10; t3[i]%=10; t3[i+1]+=g; if(i==k-1)/*最高位要进位,需要把数组长度再加1*/ k=k+1; } t2[i]=t3[i]; t3[i]=0;/*一定要把t3数组清空,否则下一次运算时,会再次加上,导致结果出错*/ } n=k;/*更新t2数组长度*/ } void tiaozhen() { int i,j; t1[0]+=1;/*每乘一次,待乘数要加1,t1[0]一定要初始化为1*/ for(i=0;i<m;i++) { if(t1[i]>9)/*逢10进1*/ { j=t1[i]/10; t1[i]%=10; t1[i+1]+=j; if(i==m-1)/*最高位要进位,需要把数组长度再加1*/ m+=1; } } } void Njie(int e) { int i,j; for(i=2;i<=e;i++) { tiaozhen(); mul(); } } void print() { int i; for(i=n-1;i>=0;i--) printf("%d",t2[i]); printf("\n"); } int main() { int i,e; while(scanf("%d",&e)==1) { if(e>1) { memset(t1,sizeof(t1)); memset(t2,sizeof(t2)); memset(t3,sizeof(t3)); n=m=1; t1[0]=1; t2[0]=1; Njie(e); print(); } else printf("1\n"); } return 0; }