POJ1032 Parliament
题目来源:http://poj.org/problem?id=1032
题目大意:给定一个正整数N(5<=N<=1000),将N拆为若干个不同的数使得它们的乘积最大(找到一组互不相等,和为N,乘积最大的正整数)。
输入:N
输出:选择的数,升序输出。
Sample Input
7
Sample Output
3 4
假设不考虑拆成不等的数这个条件,那么依我们的经验应该是拆成相等的数乘积最大。加上这个条件之后,就应该是相邻的数乘积最大。那么给定一个N时,我们希望能将它拆为尽可能小的连续的数。而选择的数一定不能小至1,因为乘1不改变目标值,起不到作用纯属浪费。所以最好拆到2.当从2开始的序列累加和到某个数即将超过N时,停下,将剩下的数由高位向低位每个数的值增加1.剩余的数最多会把所有的数都加了1,还剩1个,(如果剩的比这还多,在开始从2往上累加时是不会停下来的,由数列求和公式可知。)若还剩1则再加到最的数上去。
//////////////////////////////////////////////////////////////////////////
// POJ1032 Parliament
// Memory: 280K Time: 0MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; int main() {
int N;
cin >> N;
int cnt;
int sum = ;
for (cnt = ; sum + + cnt<= N; ++cnt) {
sum += ( + cnt);
}
int left = N - sum;
int p = + cnt;
while (left > ) {
--p;
--left;
}
if (p == ) {
for (int i =; i < + cnt; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
} else if (p == ) {
for (int i =; i < + cnt; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
} else if (p == cnt + ) {
for (int i =; i < + cnt; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
} else {
for (int i =; i <= p; ++i) {
cout << i << " ";
}
for (int i = p + ; i < cnt + ; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
}
system("pause");
return ;
}
POJ1032 Parliament的更多相关文章
- POJ1032 Parliament(数论)
New convocation of The Fool Land's Parliament consists of N delegates. According to the present regu ...
- timus 1136 Parliament(二叉树)
Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...
- timus 1136 Parliament(e)
Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...
- codeforces 644A Parliament of Berland
A. Parliament of Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Graphical Analysis of German Parliament Voting Pattern
We use network visualizations to look into the voting patterns in the current German parliament. I d ...
- Poj 1032 Parliament
Parliament Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19103 Accepted: 8101 Descr ...
- 译《The Part-Time Parliament》——终于读懂了Paxos协议!
最近的考古发现表明,在Paxos小岛上,尽管兼职议会成员都有逍遥癖,但议会模式仍然起作用.他们依旧保持了一致的会议记录,尽管他们频繁的进出会议室并且他们的信使还很健忘.Paxon议会协议提供了一种新方 ...
- 北大poj- 1032
Parliament Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18707 Accepted: 7941 Descr ...
- Code Forces 644A Parliament of Berland
A. Parliament of Berland time limit per test1 second memory limit per test256 megabytes inputstandar ...
随机推荐
- 音频压缩(Speex使用&Opus简介)--转
博客地址:http://blog.csdn.net/kevindgk GitHub地址:https://github.com/KevinDGK/MyAudioDemo 一简介 二局域网语音配置 三Sp ...
- LOJ2302 「NOI2017」整数
「NOI2017」整数 题目背景 在人类智慧的山巅,有着一台字长为$1048576$位(此数字与解题无关)的超级计算机,著名理论计算机科 学家P博士正用它进行各种研究.不幸的是,这天台风切断了电力系统 ...
- ACM学习历程—51NOD1028 大数乘法V2(FFT)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...
- java枚举学习enum
java 1.5以后才出现enum的关键字 所有的enum类都继承自Enum类,所以enum类无法再继承其他的类,可以实现接口,枚举类出了不能被继承其余的与普通类的特性一致, 枚举类的构造函数只能自己 ...
- maven的配置文件取不同版本
1. 两个地方需要配置首先是要定义配置源,dev/test/prod对应的配置源需要指定:这是灵活的,可变的:其次要定义配置文件,该配置文件里面的内容采用占位符的方式来编制,在编译打包的过程中要动态根 ...
- 51nod 1967 路径定向——欧拉回路
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1967 一共只会有偶数个奇数度的点.因为每多一条边,总度数加2. 把 ...
- Python-实现socket简单地连续发数据
只需用while条件判断来实现 服务器端 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import socket serv ...
- DevExpress 柱状图
通过构造函数,把值传递过来 public XtraInterpreterChartForm(object ds) { InitializeComponent(); datasource = ds; } ...
- J2EE 学习路线
分享一个比较好的学习网站 http://edu.51cto.com/roadmap/view/id-86.html ================================J2EE=== ...
- 删除eclipse Maven 进程 导致eclipse的workspace 启动不了
异常描述: An internal error occurred during: "reload maven project". java.lang.NullPointerExce ...