POJ-1032-拆数字

简介: Description New convocation of The Fool Land's Parliament consists of N delegates. According to the present regulation delegates should be divided in...

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

7

Sample Output

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;
#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;
}

  

相关文章
|
2月前
|
算法 测试技术
【数位dp】【动态规划】【状态压缩】【推荐】1012. 至少有 1 位重复的数字
【数位dp】【动态规划】【状态压缩】【推荐】1012. 至少有 1 位重复的数字
|
2月前
|
机器学习/深度学习 算法 测试技术
【动态规划】【子数组划分】【前缀和】1977. 划分数字的方案数
【动态规划】【子数组划分】【前缀和】1977. 划分数字的方案数
|
3月前
|
算法 测试技术
代码随想录 Day39 动态规划 LeetCode T139 单词拆分 动规总结篇1
代码随想录 Day39 动态规划 LeetCode T139 单词拆分 动规总结篇1
34 0
|
4月前
【每日一题Day257】LC2178拆分成最多数目的正偶数之和 | 贪心
【每日一题Day257】LC2178拆分成最多数目的正偶数之和 | 贪心
19 2
|
5月前
|
算法
代码随想录算法训练营第二十六天 | LeetCode 39. 组合总和、40. 组合总和 II、131. 分割回文串
代码随想录算法训练营第二十六天 | LeetCode 39. 组合总和、40. 组合总和 II、131. 分割回文串
30 0
|
10月前
|
人工智能 索引
Leecode 1013. 将数组分成和相等的三个部分
Leecode 1013. 将数组分成和相等的三个部分
50 0
|
人工智能 BI
力扣刷题记录——561. 数组拆分、566. 重塑矩阵、575. 分糖果
力扣刷题记录——561. 数组拆分、566. 重塑矩阵、575. 分糖果
114 0
力扣刷题记录——561. 数组拆分、566. 重塑矩阵、575. 分糖果
代码随想录刷题|Leetcode 39. 组合总和 40.组合总和II 131.分割回文串
代码随想录刷题|Leetcode 39. 组合总和 40.组合总和II 131.分割回文串
代码随想录刷题|Leetcode 39. 组合总和 40.组合总和II 131.分割回文串
LeetCode每日一题——902. 最大为 N 的数字组合
给定一个按 非递减顺序 排列的数字数组 digits 。你可以用任意次数 digits[i] 来写的数字。例如,如果 digits = [‘1’,‘3’,‘5’],我们可以写数字,如 ‘13’, ‘551’, 和 ‘1351315’。
76 0
LeetCode每日一题——902. 最大为 N 的数字组合
|
人工智能 BI
每日一题-两序列
每日一题-两序列
97 0