洛谷P1009阶乘之和--zhengjun

题目描述

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

输入格式

一个正整数 N N N

输出格式

一个正整数 S S 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A_zjzj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值