Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 105341 Accepted Submission(s): 31438
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
One N in one line, process to the end of file.
For each N, output N! in one line.
1
2
3
Sample Output
1
2
6
代码如下:
#include<stdio.h>
#include<string.h>
#define N 40001
int main()
{
int n;
int ans[N];
int i,j,up; // up 记录进位
int temp ;
while(~scanf("%d",&n))
{
memset(ans,0,sizeof(ans)); //把数组全置零
ans[0]=1;
for(i = 2; i <= n; i++)
{
up=0;
for(j=0;j<N;j++)
{
temp=ans[j]*i+up;
ans[j]=temp % 10;
up=temp/10;
}
}
for(i=N-1;i>=0;i--) if(ans[i]) break; // 倒置输出 过滤前面的 0
for( ; i>=0; --i)
printf("%d",ans[i]);
printf("\n");
}
return 0;
}
原文:https://www.cnblogs.com/lkfsblogs/p/12600206.html