1 /*问题:
2 猴子分桃:海 滩上有一堆桃子,五只猴子来分。
3 第一只猴子把这堆桃子平分分为五份,多了一个,
4 这只猴子把多的一个扔入海中,拿走了一份。
5 第二只猴子把剩下的桃子又平均分成五份,又多了一个,
6 它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,
7 问海滩上原来最少有多少个桃子?*/
//解法1:个人
8 public class Homework {
9 public static void main(String[] args) {
10 for (int j = 1; ; j++) {
11 boolean flag = false;
12 int n = j;// 设置最后得到数量
13 int k;//
14 k = n * 5 + 1;// 第5次分前的数量
15 for (int i = 4; i >= 1; i--) { // 执行4次
16 if (k % 4 == 0) {
17 k = k / 4 * 5 + 1;// 设置得到i次分之前的数量
18 } else {
19 continue;
20 break;
21 }
22 if (i == 1) {
23 System.out.println("第五个猴子得到" + n + "个桃子");
24 System.out.println("桃子总数为" + k);
25 flag = true;//标记已得到结果
26 }
27 }
28 if(flag){//已有结果退出循环
29 break;
30 }
31 }
32
33 }
34
35 }
1
//解法二:转csdn RoidCoder的解法
public class PeachDivideAlgorithm {
2 private static int num;//桃子总数
3 private static int count;//猴子总数,也是分桃子的次数
4 public static int getResult(int count){
5 if(count==0){
6 return 1;
7 }
8 if((num-1)%5!=0){
9 return -1;
10 }
11 num=(num-1)*4/5;
12 return getResult(count-1);
13 }
14 public static void main(String[] args){
15 count=5;
16 for(int i=0;i<10000;i++){//桃子数在10000内的穷举
17 num=i;
18 if(getResult(count)==1){
19 System.out.println("the number of the peach:"+i);
20 }
21 }
22 }
23 }