九度 oj 题目1076:N的阶乘

来源:互联网 发布:java编程那些事儿 编辑:程序博客网 时间:2024/04/28 12:55

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


#include <stdio.h>#include <string>#include <cstring>using namespace std;#define MAXN 99999typedef 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很大,那么要考虑压缩。

0 0
原创粉丝点击