题目:
例子:

思路:
分情况模拟进行解答,具体思路如下:
- 当孩子数量小于美元时,无法分发最基本的,返回-1;
- 当孩子数量大于美元数小于美元-7时,无法给一个孩子发8美元,因此返回0;
- 当美元更多时,获取美元除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;
}
}
}
}