暴力破解-分月饼

分月饼

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

输入描述:

第一行输入m n,表示m个员工,n个月饼,m<=n
1
输出描述:

输出有多少种月饼分法
代码:

public class Title {

    private static int maxIdea = 0;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String[] split = sc.nextLine().split(" ");
        int yuanGong = Integer.valueOf(split[0]);
        int yueBin = Integer.valueOf(split[1]);
        int count = yueBin - yuanGong;

        // 最少分得的数量
        int min =Math.max(0, count/yuanGong - yuanGong/2*3);
        // 最大分得的数量
        int max = count/yuanGong + yuanGong/2*3;

        int pinjun = count / yuanGong;
        // 分法数量
        List<Integer> list = new ArrayList<>();

        for (int i = min; i <= pinjun; i++) {
            list.add(i);
            bfs(count - i, max, yuanGong - 1,  i,list);
            list.remove(list.get(list.size()-1));
        }
        System.out.println(maxIdea);

    }

    private static void bfs(int count, int max, int yuangong,int i,List<Integer> list) {

        if (yuangong == 0 && count == 0) {
            maxIdea++;
            System.out.println(count+" -- "+i);
            return;
        } else if (yuangong < 0) {
            return;
        }

        for (int j = i; j <= max; j++) {
            if (j-i>3){
                continue;
            }
            count -= j;
            yuangong -= 1;
            list.add(j);
            bfs(count, max, yuangong,j,list);
            count += j;
            yuangong += 1;
            list.remove(list.get(list.size()-1));
        }
    }


}

简单运行了几个用例,和预期是一样的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值