标题:猴子分香蕉
package 省题2018;
public class 猴子分香蕉 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义变量存放最终香蕉数
int num = 0;
//遍历
for(int i = 1; i < Integer.MAX_VALUE; i++) {
//临时变量
int temp = 0;
//第1只猴子醒来,把香蕉均分成5堆,还剩下1个
if(i % 5 == 1) {
//吃掉剩下的一个并把自己的一份藏起来
temp = i - (i / 5) - 1;
//2只猴子醒来,重新把香蕉均分成5堆,还剩下2个
if(temp % 5 == 2) {
//吃掉剩下的二个并把自己的一份藏起来
temp = temp - (temp / 5) - 2;
//3只猴子醒来,重新把香蕉均分成5堆,还剩下3个
if(temp % 5 == 3) {
//吃掉剩下的三个并把自己的一份藏起来
temp = temp - (temp / 5) - 3;
//4只猴子醒来,重新把香蕉均分成5堆 还剩下4个,
if(temp % 5 == 4) {
//就吃掉剩下的四个并把自己的一份藏起来
temp = temp - (temp / 5) -4;
//第5只猴子醒来,重新把香蕉均分成5堆,正好不剩
//当temp等于0时,temp % 5 也等于0;所以这里要设置temp != 0
if(temp % 5 == 0 && temp != 0) {
num = i;
break;
}else {
continue;
}
}else {
continue;
}
}else {
continue;
}
}else {
continue;
}
}else {
continue;
}
}
System.out.println(num);
}
}