前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ-1032-拆数字

POJ-1032-拆数字

作者头像
瑾诺学长
发布2018-09-21 16:13:08
4500
发布2018-09-21 16:13:08
举报
文章被收录于专栏:专注研发专注研发

Description

New convocation of The Fool Land's Parliament consists of N delegates. According to the present regulation delegates should be divided into disjoint groups of different sizes and every day each group has to send one delegate to the conciliatory committee. The composition of the conciliatory committee should be different each day. The Parliament works only while this can be accomplished. You are to write a program that will determine how many delegates should contain each group in order for Parliament to work as long as possible.

Input

The input file contains a single integer N (5<=N<=1000 ).

Output

Write to the output file the sizes of groups that allow the Parliament to work for the maximal possible time. These sizes should be printed on a single line in ascending order and should be separated by spaces.

Sample Input

代码语言:javascript
复制
7

Sample Output

代码语言:javascript
复制
3 4
题目大意:
将整数n,(5<=n<=1000)拆成多个不相等的数(和),使所以有的数之积最大。
思路;
一个数n入能被2 整除,则(n/2)*(n/2)最大;若不能则(n/2)*(n/2+1)最大
所以启发:把一个数从小的开始分,把剩余的部分分别加到后面大数上
所有数开始拆:
例如 7:先拆成:2  3 剩余2 ,分别让2,3加1,得3 4;
例如 8:拆成2 3 剩余3;分别让2 3 加1 ,还余1;让3加1再加1;得3 5;
例如 9 :直接拆成2 3 4;
代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{

    int n,s=0,a[100]={0};
    cin>>n;
    int i;
    for(i=2;s+i<=n;i++)//注意不能仅仅让s<=n;这样会导致多循环一次
    {
      a[i]=i;
      s+=i;
    }

    int h=i;
    for(int j=n-s;j>0;j--)//把剩余的即n-s部分从数组最后往前均分
    {
        a[--i]++;
        if(i==2)i=h;//第一遍均分后让i回到最后再开始均分

    }
for(int k=2;k<h;k++)
{
    cout<<a[k]<<" ";
}
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档