ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

洛谷P1009阶乘之和--zhengjun

2022-06-10 18:34:25  阅读:121  来源: 互联网

标签:bignum -- operator return int num 阶乘 洛谷 const


题目描述

用高精度计算出\(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

标签:bignum,--,operator,return,int,num,阶乘,洛谷,const
来源: https://www.cnblogs.com/A-zjzj/p/16364284.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有