poj 1032 Parliament 【思维题】

题目地址:http://poj.org/problem?id=1032

Parliament
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17473   Accepted: 7371

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从2开始进行分解,注意:每个被分解出来的数都必须是不同的。比如:6= 3+3; 3,3是不合法的!
(1).若 n = 2 + 3 + ... + k + k ,不够减的数余下为k, 则将其转换成:n=3+4+..+k+(k+2);
比如:13=2+3+4+4,则转换成:13=3+4+6
(2).若不够减的数余下小于k,则将余下的n,从最大的数开始往前分配1。
比如: 10=2+3+4+1; 则转换成:10=2+3+5;
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    int i, j;
    int a[1010];
    int e=0;

    scanf("%d", &n);

    for(i=2; i<=n; i++)
    {
        a[e++]=i;
        n=n-i;
    }
    if(n<a[e-1] && n>0 )
    {
        for(i=e-1; i>=0; i--)
        {
            a[i]++;
            n--;
            if(n==0) break;
        }
    }
    else if(n==a[e-1])
    {
        for(i=0; i<e; i++) a[i]++;
        a[e-1]++;
    }
    for(i=0; i<e; i++)
    {
        if(i==0) printf("%d", a[i]);
        else printf(" %d", a[i]);
    }
    printf("\n");
    return 0;
}

 


posted @ 2015-07-21 20:58  我喜欢旅行  阅读(372)  评论(0编辑  收藏  举报