1032:Parliament

整数分解:
将整数N分解任意个不同的整数,使这些整数的乘积最大。

数学分析
参考:https://blog.csdn.net/kongming_acm/article/details/6212813

代码思路:
设此最大序列为2、3、……、w,则:
1.若剩余值(n-sum)等于w,则最后输出序列为:3、4、……、w、w+2,即将原最大序列每项加1,再将最后剩的一个1加到最后一项上。
2.若剩余值(n-sum)小于w,则从序列的最大项i开始,从大到小依次将每项加1,直到剩余值用完。

//整数拆分
#include <iostream>
using namespace std;

int ans[100]={0};

int main() {
	int n;
	cin >> n;
	int sum = 0, i;
	for (i = 2; sum + i <= n; i++) {
		sum += i;
	}
	i--;
	if (sum + i == n) {
		for (int j = 2; j < i; j++) {
			cout << j + 1 << " ";
		}
		cout << i + 2<<endl;
	}
	else {
		int cnt = n - sum;
		for (int j = i; j >= 2; j--) {
			if (cnt > 0) {
				ans[j] = j + 1;
				cnt--;
			}				
			else {
				ans[j] = j;
			}
		}
		cout << ans[2];
		for (int j = 3; ans[j] != 0; j++)
			cout << " " << ans[j];
		cout << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值