输入不超过1000的正整数n,输出n!的精确结果
方法:使用数组f保存,让f[0]保存结果的个位、f[1]是十位...手动模拟计算n!,输出时注意忽略前导0
#include<stdio.h>
#include<string.h>
const int maxn = 3000;
int f[maxn];
int main()
{
int i,j,n;
scanf("%d",&n);
memset(f,0,sizeof(f));
f[0] = 1;
for(i = 2;i <=n;i++)
{
int c = 0;
for(j = 0; j < maxn; j++)
{
//core computing
int s = f[j]*i + c;
f[j] = s % 10;
c = s / 10;
}
}
for(j = maxn - 1; j >=0; j--) if(f[j]) break;
for(i = j; i >= 0; i--) printf("%d",f[i]);
printf("\n");
return 0;
}