九度 oj 题目1076:N的阶乘

62 篇文章 0 订阅
5 篇文章 0 订阅

http://ac.jobdu.com/problem.php?pid=1076


#include <stdio.h>
#include <string>
#include <cstring>

using namespace std;
#define MAXN 99999
typedef struct bign{ 
    int d[MAXN]; 
    bign(string s){ 
        int len = (int) s.size() ;
        for (int i = len-1; i>=0; i--) { 
            d[i+1] =s[(unsigned)i] - '0'; 
        } 

       d[0] = len;  
       while(d[0]> 0 && d[d[0]] == 0 ) d[0]--; 
    } 
    bign(){ 
        *this = bign("0"); 
    }  
    string toString(){ 
       string s("");  
       for (int i = d[0]; i>=1;--i) { 
           s = s + (char) (d[i]+'0'); 
       } 
       return s;
    } 

} Bign; 

Bign operator * (const Bign & a, int b){ 
    Bign c; 
    c.d[0] = a.d[0];
    int x =0;
    for (int i = 1; i <=a.d[0]; ++i) { 
        x = x + a.d[i]*b; 
        c.d[i] = x%10;
        x = x/10;
    } 

    while(x!=0){ 
        c.d[++c.d[0]] = x%10;
        x /= 10; 
    }  
    return c;
}  


int main(){ 
    int n;
    while(scanf("%d",&n)!=EOF ){ 
        Bign sum("1"); 
        for (int i = 2; i <=n; ++i) { 
            sum = sum * i; 
        } 
        string reString = sum.toString();
        printf("%s\n",reString.c_str());
    }   
}  


注意因为 n最大到1000,所以较小的MAXN可以cover住,如果n很大,那么要考虑压缩。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值