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很大,那么要考虑压缩。