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
首先存数:a[0]=4372,a[1]=792,a[2]=1。107924372,从低位到高位每四位存到一个数组元素中。此时,总位数为3。
接着运算:a[0]*15=65580,所以进位为a[0]/10000=6,a[0]=a[0]%10000=5580。a[1]*15=11880,a[1]=a[1]+6=11886。
进位为1,a[1]=1886。a[2]*15=15,a[2]=a[2]+1=16,进位为0。
输出:a[2],a[1],a[0]即为1618865580。要注意的是:如果a[2]=886,那么该如何输出?直接输出:168865580。显然不对,
正确的是16088655880。输出的原则是:最高位原样输出,其它位如果小于1000,则高位补0,一位一补。
#include<stdio.h>
int main()
{
int a[10001];
int n,p,c,i,j;
while(scanf("%d",&n)!=EOF)
{
p=1;a[1]=1;
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=p;j++)
{
a[j]=a[j]*i+c;
c=a[j]/10000;
a[j]%=10000;
}
if(c>0)
{
p++;
a[p]=c;
}
}
if(a[p]) printf("%d",a[p]);
for(i=p-1;i>=1;i--)
printf("%.04d",a[i]);
printf("\n");
}
return 0;
}