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
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
1 2 3
1 2 6
题解:大数阶乘,注意要记录结果长度,如此可方便输出,减少计算量。
#include<stdio.h>
#include<string.h>
int res[40000];
int main()
{
int num,length;
while(~scanf("%d",&num))
{
/*重置部分*/
memset(res,0,sizeof(res));
/*计算部分*/
res[0]=1;
length=0;
for(int i=2;i<=num;i++)
{
for(int j=0;j<=length;j++) res[j]*=i;
/*进位部分*/
for(int i=0;i<=length;i++)
if(res[i]>9)
{
res[i+1]+=res[i]/10;res[i]%=10;
if(i+1>length){length++;} /*更新结果长度*/
}
}
/*输出部分*/
for(int k=length;k>=0;k--)
printf("%d",res[k]);
printf("\n");
}
}