题目来源: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的更多相关文章

  1. POJ1032 Parliament(数论)

    New convocation of The Fool Land's Parliament consists of N delegates. According to the present regu ...

  2. timus 1136 Parliament(二叉树)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  3. timus 1136 Parliament(e)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  4. codeforces 644A Parliament of Berland

    A. Parliament of Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. Graphical Analysis of German Parliament Voting Pattern

    We use network visualizations to look into the voting patterns in the current German parliament. I d ...

  6. Poj 1032 Parliament

    Parliament Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19103   Accepted: 8101 Descr ...

  7. 译《The Part-Time Parliament》——终于读懂了Paxos协议!

    最近的考古发现表明,在Paxos小岛上,尽管兼职议会成员都有逍遥癖,但议会模式仍然起作用.他们依旧保持了一致的会议记录,尽管他们频繁的进出会议室并且他们的信使还很健忘.Paxon议会协议提供了一种新方 ...

  8. 北大poj- 1032

    Parliament Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18707   Accepted: 7941 Descr ...

  9. Code Forces 644A Parliament of Berland

    A. Parliament of Berland time limit per test1 second memory limit per test256 megabytes inputstandar ...

随机推荐

  1. Smali文件添加try/catch语句,出现“invalid use of move-exception”异常

    插入代码如下: 捕获到以下异常: 2019-03-18 21:09:35.431 8272-8272/com.xxxx.xxxx E/AndroidRuntime: FATAL EXCEPTION: ...

  2. 向vivi中加入命令

    在vivi的lib/command.c中添加自己的命令 核心数据结构user_command. typedef struct user_command { const char *name;      ...

  3. Sublime Text 全程指南(转载)

    摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的Sublime Text中文教程. 更新记录 2014/09/27:完成初稿 2014/09/28: 更正打开控 ...

  4. 【转】Pro Android学习笔记(二四):用户界面和控制(12):Style和Theme

    目录(?)[-] 静态格式 代码中设定 Style Theme 静态格式 在res/values中设置静态的Style,在资源中设置静态Style可使用的HTML格式有<i> <u& ...

  5. Myeclipse如何使用Maven添加jar包

    很多新手都不知道如何在maven项目里添加jar包. 以前我还没接触maven的时候下载过一个demo,是maven项目. 我居然是照着他的pom.xml文件一个一个的写!!! 很多人认为理所当然的东 ...

  6. windows 创建和调用 动态库,静态库

    windows创建和调用静态库 // MathFuncsLib.h namespace MathFuncs { class MyMathFuncs { public: // Returns a + b ...

  7. docker Get started part 4: Accessing your cluster cannot curl

    1. 问题描述 docker Get started part 4 can't visit myvm1 or myvm2. curl: (7) Failed to connect to 192.168 ...

  8. HTML5 中文乱码

    <meta charste="utf-8"> 只是告诉浏览器要用utf-8来解释,而文档的编码,是在你保存时的选择决定的.如果保存ANSI 然后用utf-8解释,肯定是 ...

  9. Servlet包介绍

    ----------------siwuxie095                         首先到 Tomcat 的官网下载 Tomcat 的 API 帮助文档     Tomcat 官网: ...

  10. 杭电acm 1034题

    Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...