M个人分N个月饼

中秋节,公司分月饼,m个员工,买了n个月饼,m<=n,每个员工至少分1个月饼,但可以分多个,单人份到最多月饼的个数为Max1,单人分到第二多月饼的个数是Max2,Max1-Max2<=3,。同理,单人分到第n-1多月饼的个数是Max(n-1),单人分到第n多月饼的个数是Max(n),Max(n-1)-Max(n)<=3。请问有多少种分月饼的方法?

#include <stdio.h>
#include <vector>

std::vector<int> vec;

int dispatch(int nPeople, int nMoon, int nMin) {
    if (nMoon <= 0)
    {
        return 0; // 分配失败
    }

    if (nPeople <= 0)
    {
        return 0;
    }
    if (nPeople == 1)
    {
        if (nMoon >= nMin && nMoon <= nMin + 3)
        {
            vec.push_back(nMoon);
            for (std::vector<int>::iterator x = vec.begin(); x != vec.end(); x ++)
            {
                printf("%d ", *x+1);
            }
            printf("\n");
            vec.pop_back();
            return 1;
        }
        return 0;
    }

    int nCount = 0;
    for (int got = nMin; got <= nMin+3; got++)
    {
        vec.push_back(got);
        nCount += dispatch(nPeople-1, nMoon-got, got);
        vec.pop_back();
    }
    return nCount;
}


int main(void)
{
    int nPeople = 10, nMoon = 34;
    nMoon -= nPeople;

    int nCount = 0;
    // 先确定分得月饼最少的那个人得到的月饼数
    for (int got = 0; got <= nMoon; got++) // got表示分到月饼最少的那个人得到的月饼数
    {
        vec.push_back(got);
        nCount += dispatch(nPeople-1, nMoon-got, got); // 第二少的人只能分[got,got+3]个
        vec.pop_back();
    }

    printf("%d\n", nCount);

    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值