阶乘和

4、阶乘和(sum.pas)
【问题描述】
已知正整数N(N<=100),设S=1!+2!+3!+…N!。其中”!”表示阶乘,即N!=1*2*3*……*(N-1)*N,如:3!=1*2*3=6。请编程实现:输入正整数N,输出计算结果S的值。
【输入样例】sum.in
4
【输出样例】sum.out
33

#include
#include
#include
#include
using namespace std;
const int MAXN=40000;
struct BIGNUM {
int len,s[MAXN];
BIGNUM () {memset(s,0,sizeof(s)); len=1;}
BIGNUM operator = (const char* num) {
len=strlen(num);
for (int i=0;i1]-'0';
return *this;
}
BIGNUM operator = (const int num) {
char a[MAXN];
sprintf(a,"%d",num);
*this = a;
return *this;
}
BIGNUM (const int num) { *this=num; }
BIGNUM (const char * num) { *this=num; }
BIGNUM operator + (const BIGNUM & a) {
BIGNUM c;
c.len=max(len,a.len)+1;
for (int i=0,x=0;i10;
c.s[i]=c.s[i]%10;
}
if (c.s[c.len-1]==0) --c.len;
return c;   
}
BIGNUM operator += (const BIGNUM & a) {
*this = *this+a;
return *this;
}
BIGNUM operator * (const BIGNUM & x) {
BIGNUM c;
c.len=len+x.len;
for (int i=0;ifor (int j=0;j1]+=c.s[i+j]/10;
c.s[i+j]%=10;
}
}
if (c.s[c.len-1]==0) --c.len;
return c;
}    
BIGNUM operator *= (const BIGNUM & a) {
*this = *this * a;
return *this;
}
bool operator < (const BIGNUM & x) const {
if (len != x.len) return lenfor (int i=len-1;i>=0;--i) {
if (s[i] != x.s[i]) return s[i]return false;
}
bool operator > (const BIGNUM & x) const { return x<*this; }
bool operator <= (const BIGNUM & x) const { return !(x<*this); }
bool operator >= (const BIGNUM & x) const { return !(*thisbool operator == (const BIGNUM & x) const { return !(x<*this||*thisbool operator != (const BIGNUM & x) const { return x<*this||*thisoperator << (ostream &out,const BIGNUM& x) {
for (int i=x.len-1;i>=0;--i)
cout<return out;
}
istream& operator >> (istream &in,BIGNUM &x) {
char num[MAXN];
in>>num;
x=num;
return in;
}
BIGNUM f[5001];
BIGNUM jie(int x){
    BIGNUM s=1;
    for (int i=1;i<=x;i++) s*=i;
    return s;
}
int main ()
{
    int n; BIGNUM sum=0;
    cin>>n;
    for (int i=1;i<=n;i++) sum+=jie(i);
    cout<return 0;
}

重载,不解释

你可能感兴趣的:(信息学奥赛一本通上的水题)