题意:求大数N的阶乘,然后要求输出的是阶乘的结果从第一位起不是0的数。
思路:重要的是求大数N的阶乘,这以前我在COJ的时候专门练过n!所以算法依然清晰……直接贴代码,不理解的用例子算算就明白了……
#include<iostream>
#include<cstring>
#include<cstdio>
#define M 3000
int f[M];
using namespace std;
int main()
{
int j,i,n,c,s;
while(cin>>n)
{
memset(f,0,sizeof(f));
f[0]=1;
for(i=2;i<=n;i++)
{
c=0;
for(j=0;j<M;j++)
{
s=f[j]*i+c;
f[j]=s%10;
c=s/10;
}
}
for(j=0;j<M;j++)
if(f[j]) {printf("%5d -> %d\n",n,f[j]);break;}
}
return 0;
}