• POJ-1032-拆数字


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

      

  • 相关阅读:
    Average of Levels in Binary Tree
    Maximum Average Subarray I
    Integer Replacement
    Add Digits
    Binary Tree Level Order Traversal II
    Majority Element II
    Majority Element
    匿名函数
    Django的Template不支持range()函数的问题解决办法
    python文件对比利用difflib库实现文件夹下详细内容对比
  • 原文地址:https://www.cnblogs.com/jin-nuo/p/5288975.html
Copyright © 2020-2023  润新知