洛谷P1009阶乘之和--zhengjun

题目描述

用高精度计算出\(S=1!+2!+3!+…+n! (n\le 50)\)
其中\(“!”\)表示阶乘,例如:\(5!=5 \times 4 \times 3 \times 2 \times 1\)

输入格式

一个正整数\(N\)

输出格式

一个正整数\(S\),表示计算结果。

输入输出样例

输入 #1 复制
3
输出 #1 复制
9

思路

高精度啦,用运算符重载。不会的就用数组模拟。

代码

#include<bits/stdc++.h>
#define maxn 10005
using namespace std
/*********************以下是模板*******************/
struct bignum {
	int len,s[maxn];
	char flag;
	bignum() {
		len=1;
		flag='+';
		memset(s,0,sizeof(s));
	}
	bignum (int num) {
		*this=num;
	}
	bignum (const char *num) {
		*this=num;
	}
	bignum operator = (const char *a) {
		len=strlen(a);
		for (int i=1; i<=len; ++i)
			s[i]=a[len-i]-'0';
		return *this;
	}
	bignum operator = (const int num) {
		char a[maxn];
		sprintf(a,"%d",num);
		*this=a;
		return *this;
	}
	bignum operator + (const bignum &a) {
		bignum c;
		c.len=max(len,a.len)+1;
		for (int i=1; i<c.len; ++i) {
			c.s[i]+=(s[i]+a.s[i]);
			c.s[i+1]+=c.s[i]/10;
			c.s[i]%=10;
		}
		if (c.s[c.len]==0)
			c.len--;
		return c;
	}
	bignum operator += (const bignum &a) {
		*this=*this+a;
		return *this;
	}
	bignum operator * (const bignum &a) {
		bignum c;
		c.len+=(len+a.len);
		for (int i=1; i<=len; ++i)
			for (int j=1; j<=a.len; ++j) {
				c.s[i+j-1]+=(s[i]*a.s[j]);
				c.s[i+j]+=(c.s[i+j-1]/10);
				c.s[i+j-1]%=10;
			}
		while (c.s[c.len]==0)
			c.len--;
		return c;
	}
	bignum operator *= (const bignum &a) {
		*this=(*this) * a;
		return *this;
	}
	bool operator < (const bignum &a) const {
		if (len!=a.len)
			return len<a.len;
		for (int i=len; i>=1; --i)
			if (s[i]!=a.s[i])
				return s[i]<a.s[i];
		return false;
	}
	bool operator > (const bignum &a) const {
		return a<*this;
	}
	bool operator <= (const bignum &a) const {
		return !(*this>a);
	}
	bool operator >= (const bignum &a) const {
		return !(*this<a);
	}
	bool operator == (const bignum &a) const {
		return !((*this<a) || (*this>a));
	}
	bool operator != (const bignum &a) const {
		return !(*this==a);
	}
	void change (bignum &a,bignum &b) {
		bignum tmp=a;
		a=b;
		b=tmp;
	}
	bignum operator - (const bignum &a) const {
		bignum b=*this,c;
		if (b<a) {
			c.flag='-';
			c.len=a.len;
			for (int i=1; i<=c.len; ++i) {
				c.s[i]+=(a.s[i]-b.s[i]);
				if (c.s[i]<0) {
					c.s[i]+=10;
					c.s[i+1]-=1;
				}
			}
			while (c.len==0)
				c.len--;
			return c;
		}
		c.len=b.len;
		for (int i=1; i<=c.len; ++i) {
			c.s[i]+=(b.s[i]-a.s[i]);
			if (c.s[i]<0) {
				c.s[i]+=10;
				c.s[i+1]-=1;
			}
		}
		while (c.len==0)
			c.len--;
		return c;
	}
	bignum operator -= (const bignum &a) {
		*this=(*this)-a;
		return *this;
	}
	bignum operator / (const int n) {
		bignum c,b=*this;
		c.len=b.len;
		int x=0;
		for (int i=1; i<=n; ++i) {
			c.s[i]=(x*10+b.s[i])/n;
			x=(x*10+b.s[i])%n;
		}
		while (c.s[c.len]==0)
			c.len--;
		return c;
	}
	bignum operator /= (const int a) {
		*this=*this/a;
		return *this;
	}
};
ostream& operator << (ostream &out,const bignum &x) {
	for (int i=x.len; i>=1; --i)
		printf("%d",x.s[i]);
	return out;
}
/*******************以上是模板*********************/
int n;
bignum sum,ans;
int main() {
	scanf("%d",&n);
	sum=1;
	for(int i=1; i<=n; i++) {
		sum*=i;
		ans+=sum;
	}
	cout<<ans;
	return 0;
}

谢谢--zhengjun

posted @ 2022-06-10 18:27  A_zjzj  阅读(25)  评论(0编辑  收藏  举报