求阶乘。
最多1000;
必须先把每个阶乘用字符串存好,要不然会超时。。
用高精度乘法从一开始算出每个值。然后询问直接输出
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int MAXN=3000;
const int M=1000+10;
string sol[M];
char fact[MAXN];
int len;
void store(int n)
{
for(int i=len-1;i>=0;i--) sol[n]+=(char)(fact[i]+'0');
}
void solve(int n)
{
len=1;fact[0]=1;
store(1);store(0);
int s,c=0;
for(int i=2;i<=n;i++){
for(int j=0;;j++){
s = fact[j]*i+c;
fact[j] = s%10;
c = s/10;
if(j>=len-1 && c==0){
len = j+1;
break;
}
}
store(i);
}
}
int main(){
freopen("out2.txt","w",stdout);
solve(1000);
//for(int i=1;i<=1000;i++) printf("%s\n",sol[i].c_str());
int n;
while(~scanf("%d",&n)){
printf("%d!\n%s\n",n,sol[n].c_str());
}
return 0;
}