2591 将钱分给最多的儿童

题目:

在这里插入图片描述

例子:
在这里插入图片描述

思路:

分情况模拟进行解答,具体思路如下:

  1. 当孩子数量小于美元时,无法分发最基本的,返回-1;
  2. 当孩子数量大于美元数小于美元-7时,无法给一个孩子发8美元,因此返回0;
  3. 当美元更多时,获取美元除8的余数,通过对余数进行判断即可,区分条件,计算不同情况;

代码:

class Solution {
    public int distMoney(int money, int children) {
        if(children+7>money){
            if(children>money) return -1;
            else return 0;
        }
        int quotient = money/8;
        int res = money%8;
        if(quotient>children) return children-1;
        else if(quotient==children && res==0) return children;
        else if(quotient==children && res!=0) return children-1;
        else {
            int reschildren = children-quotient;
            if(reschildren==1 && res==4) return quotient-1;
            if(reschildren>res){
                int i = 0;
                while (res+i*7<reschildren){
                    i++;
                    quotient--;
                }
                return quotient;
            }else {
                return quotient;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值