753 A. Santa Claus and Candies

本文介绍了一个关于圣诞老人如何将不同数量的糖果分配给最多数量的孩子的问题,并提供了一种算法解决方案。该问题要求每个孩子获得的糖果数量各不相同且为正整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Santa Claus and Candies
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Santa Claus has n candies, he dreams to give them as gifts to children.

What is the maximal number of children for whose he can give candies if Santa Claus want each kid should get distinct positive integer number of candies. Santa Class wants to give all n candies he has.
Input

The only line contains positive integer number n (1 ≤ n ≤ 1000) — number of candies Santa Claus has.
Output

Print to the first line integer number k — maximal number of kids which can get candies.

Print to the second line k distinct integer numbers: number of candies for each of k kid. The sum of k printed numbers should be exactly n.

If there are many solutions, print any of them.
Examples
Input

5

Output

2
2 3

Input

9

Output

3
3 5 1

Input

2

Output

1
2

#include<stdio.h>
#define maxn 1000
int a[maxn];
void init();
int main()
{
    init();
    int n,i;
    while(~scanf("%d",&n))
    {
        if(n==1||n==2)
        {
            printf("%d\n",1);
            printf("%d\n",n);
            continue;
        }
        printf("%d\n",a[n]);
        int sum=0;
        for(i=1;i<a[n];i++)
        {
            sum+=i;
            printf("%d ",i);

        }
        printf("%d\n",n-sum);


    }
    return 0;
}

void init()
{
    int s=1;
    int i,j;
    for(i=1;i<=1000;i+=s)
    {
        for(j=i;j<i+s+1;j++)
            a[j]=s;
        s++;
    }///n分成若干个不相等的数

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值