uva 623 - 500!

求阶乘。

最多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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值