题目地址:http://poj.org/problem?id=1032
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 17473 | Accepted: 7371 |
Description
Input
Output
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;
}
原文:http://www.cnblogs.com/yspworld/p/4665609.html