基于PHP和AJAX创建RSS聚合器 移动IP与DHCP、VPN等技术的比较 五招让Vista电脑更具个性化 Oracle 如何搞垮他的数据库——谈Oracle安全

猴子吃桃问题

[ 3164 查看 / 102 回复 ]

昨天在这看到道题,想了一半还是没想出来,有哪位又答案么? 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
本主题由 管理员 admin 于 2010-3-23 19:48:21 执行 设置精华/取消 操作
TOP

猴子吃桃问题

奇怪,第一只猴子已经拿走了一份,那第二只猴只要分四份就够了,为什么还要再分成五份呢?
TOP

猴子吃桃问题

重点不是在猴子   而考你的逻辑
TOP

猴子吃桃问题

               
Java code

int num = 1;
       
for (int i = 0; i < 5; i++){
            num
=num*5+1;
        }

                System.out.println(num);
//3906

TOP

猴子吃桃问题

居然2种结果不一样
 
       
Java code

for (int i = 0; i < 5000; i++) {
           
double x = i;
           
if ((x - 1) % 5 == 0) {
                x
= (x - 1) * 0.8;
               
               
if ((x - 1) % 5 == 0) {
                    x
= (x - 1) * 0.8;
                   
                   
if ((x - 1) % 5 == 0) {
                        x
= (x - 1) * 0.8;
                       
                       
if ((x - 1) % 5 == 0) {
                            x
= (x - 1) * 0.8;
                           
                           
if ((x - 1) % 5 == 0) {
                               
                               
break;
                            }
                        }
                    }
                }
            }
        }

    3121
TOP

猴子吃桃问题

倒推啊..最少的话
那么最后一只猴子分之前是6个桃子(5份,每份1个,多1个扔海里)
那么这6个桃子就是倒数第二只猴子分桃子后的其中一份
那么倒数第二只猴子分前应该是6*5+1=31个
...
类推
TOP

猴子吃桃问题

第一种是错的...
TOP

猴子吃桃问题

先把他的算法写出来    就容易了
TOP

猴子吃桃问题

Java code

        double sum=0.1;
       
for(int i=1;sum%1!=0;i+=5){
            sum
=i;
           
for(int n=1;n<=4;n++){       
                sum
= sum/4*5+1;
            }
           
if(sum%1==0)System.out.println(i);
        }
        System.out.println(sum);

TOP

猴子吃桃问题

Java code


public class Test1 {

   
public Test1(int i){
       
this.sum = i;
    }
       
public  int sum;
   
class NoFitNumException extends Exception{
       
public NoFitNumException(){
        }
       
public NoFitNumException(String str){
           
super(str);
        }
    }
   
public  int  method41(int i) throws NoFitNumException{
       
if(i==1){
           
if((sum-1)%5!=0)
               
throw new NoFitNumException("not fit");
           
else
               
return (sum-1)/5;
        }
else {
           
if((method41(i-1)-1)%5!=0)
               
throw new NoFitNumException();
           
else
               
return (method41(i-1)-1)/5;
        }
    }
   
static  void method4(){
       
for(int out=20;out<10000;out++){
            Test1 t1
= new Test1(out);
           
try{
               
for(int i=1;1<=5;i++)
                    t1.method41(i);
               
//System.out.print(out);
            }catch(NoFitNumException e){
                System.out.println(out);
                t1
=null;
               
continue;
            }
        }
    }
   
public static void main(String[] args) {
       
// TODO Auto-generated method stub
            Test1.method4();
    }
}



代码自己写出来了,不过因为注释的那一行不能加进去,加进去报错,所以不能打印出符合要求的数字来,因此只能在catch里打印不符合要求的数字来。哪位能改进下
TOP

猴子吃桃问题

LZ你写的这个才是对的 if ((x - 1) % 5 == 0) {
                x = (x - 1) * 0.8;
                
                if ((x - 1) % 5 == 0) {
                    x = (x - 1) * 0.8;
                    
                    if ((x - 1) % 5 == 0) {
                        x = (x - 1) * 0.8;
                        
                        if ((x - 1) % 5 == 0) {
                            x = (x - 1) * 0.8;
                            
                            if ((x - 1) % 5 == 0) {
                                
                                break;
 
TOP

猴子吃桃问题

Java code


for (int i = 0;; i++) {
   
int sum = i;
   
for (int j = 0; j < 5; j++) {
       
if (sum % 5 == 1)
            sum
= sum / 5;
       
else
            sum
= 0;
    }
   
if (sum != 0) {
        System.out.println(i);
       
break;
    }
}

TOP

猴子吃桃问题

逆过来想就行了最后一回应该是6个,也就是5*1+1个倒数第2回应该是31也就是 5*6+1。。。。列出式子 就是5*((5*1)+1)+1 就这样向外宽展,就都不写出来了,运用这个规律写成循环就是:
int cont=1;
for(int i=0;i <5;i++){
    cont = 5*cont+1
}
TOP

猴子吃桃问题

啊,错了最后一回应该是1个所以,cout的初始值应该是0
int cont=0
for(int i = 0;i <5;i++){
    cont = 5*cont+1
}
TOP

猴子吃桃问题

果然..写错了- -#
TOP

猴子吃桃问题

再来一次..这次没错了..
Java code


public static void main(String[] args){
   
for (int i = 0;; i++) {
       
int sum = i;
       
for (int j = 0; j < 5; j++) {
           
if (sum % 5 == 1)
                sum
= (sum / 5)*4;
           
else
                sum
= 0;
        }
       
if (sum != 0) {
            System.out.println(i);
           
break;
        }
    }
}

TOP

猴子吃桃问题

3121?
                  int i = 0;// 结果
        int temp = 0;
        boolean cs = true;
        while (cs) {
            temp = i;
            if (temp % 5 == 1) {
                temp = (temp - 1) /5*4;
                if (temp % 5 == 1) {
                    temp = (temp - 1) /5*4;
                    if (temp % 5 == 1) {
                        temp = (temp - 1) /5*4;
                        if (temp % 5 == 1) {
                            temp = (temp - 1) /5*4;
                            if (temp % 5 == 1) {
                                if (temp != 1) {//第5次扔完桃子剩5个
                                    cs = false;
                                } else {
                                    i++;
                                }
                            } else {
                                i++;
                            }
                        } else {
                            i++;
                        }
                    } else {
                        i++;
                    }
                } else {
                    i++;
                }
            } else {
                i++;
            }
        }
        System.out.println(i);
TOP

猴子吃桃问题

楼上的什么原理呀,没看懂
TOP

猴子吃桃问题

另一种算法:
假定最后一个猴子拿到的桃子是last,然后向前倒推,看是否满足条件
 
public class Monkey {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int total ;
        int i;
        for (int last =1;last  <1000;last++)
        {
            total = (last*5)+1;
            for ( i= 0; i  < 4; i++){
                if (total%4!=0) break;
                total = total + total/4 +1;
            }
            if (i==4) {
                System.out.println("Total number is "+total);
                break;
            }
        }
    }
}
TOP

猴子吃桃问题

刚才的那个错了,没太看清题啊
这个应该没问题吧
    public static void main(String[] args) {
        
        float cont = 0;        //声明表示最后个数的变量
        int num = 5;     //最后可能存在的最小数
        for(int i=1;i>0;i++){
            cont=num++;    
            int sign_1 = 0; //声名一个表识
            for(int j=0;j <5;j++){
                cont = cont*5/4;
                if(cont%1==0){
                    cont++;
                }else{
                    sign_1++;
                    break;
                }
            }
            if(sign_1==0){
                break;
            }
        }
        System.out.println(cont);
        //3121个
    }
TOP