一道笔试题目:
海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子? 

解决方案 »

  1.   

    public class test
    { /**
     * @param args
     */
    public static void main(String[] args)
    {
    int n = 0;    //最后一个猴子分到的桃子
    int sum = 0;  //总桃子数
    int a[] = new int[5];   //分别存放五个猴子拿到的桃子数
    boolean tag = false;    
    for (int i = 1; i < 5000; i++)
    {
    n = i;
    for (int j = 0; j < 4; j++)
    { if ((n * 5 + 1) % 4 != 0)
    {
    break;
    } n = (n * 5 + 1) / 4;
    if (j == 3)
    {
    tag = true;
    }
    }
    if (tag == true)
    {
    n = i;
    a[4] = n;
    break;
    } } for (int k = 3; k >= 0; k--)
    { n = (n * 5 + 1) / 4;
    a[k] = n; }
    sum = 5 * n + 1;
    System.out.println("一共有" + sum + "个桃子");
    for (int i = 0; i < 5; i++)
    {
    System.out.println("第" + (i + 1) + "只猴子分到" + a[i] + "个桃子");
    } }
    }
      

  2.   


    public class Peaker { public void count(){
    int sum =0;//一共有几多
    int[] per = new int[5];//每只猴子分的桃子数
    per[4] = 1;//要求最少的桃子数,既最后一只猴子分到1只桃子
    for(int i=3;i>=0;i--){
    per[i]=per[i+1]*5+1;
    }
    sum = per[0]*5+1;
    System.out.println("每只分别至少分到:"+per[0]+" "+per[1]+" "+per[2]+" "+per[3]+" "+per[4]+"桃子 至少总共:"+sum);
    }
    public static void main(String[] args) {
    new Peaker().count();
    }
    }
      

  3.   


    public static void main(String[] args) {
    int peaches = 0;
    boolean hit = false;
    for (int i = 0; i < 4000; i++) {
    peaches = i;
    for (int j = 0; j < 5; j++) {
    if ((peaches - 1) % 5 != 0) {
    break;
    }
    peaches = (peaches - 1) * 4 / 5;
    if (j == 4) {
    hit = true;
    }
    }
    if (hit == true) {
    System.out.println("peaches = " + i);
    break;
    } } }
      

  4.   

    早上睁开眼友想了想效率,还是从剩余的桃子入手好 public static void main(String[] args) {
    int remainder = 0;
    boolean hit = false;
    for (int i = 0; i < 4000; i++) {
    remainder = i;
    for (int j = 0; j < 5; j++) {
    if (remainder % 4 != 0) {
    break;
    }
    remainder = remainder * 5 / 4 + 1;
    if (j == 4) {
    hit = true;
    }
    }
    if (hit == true) {
    System.out.println("peaches = " + remainder);
    break;
    } } }
      

  5.   

    public  void countMonkeySharePench()
    {
    //最少的时候当然是第五个猴子只取到一个的时候
    int count=1;
    for(int i=1;i<5;i++)
    {
    count=count*5+1;
    System.out.println(count);
    }
    System.out.println("all the pench is :"+count);
    }
      

  6.   

    6
    31
    156
    781
    all the pench is :781
      

  7.   

    上面的算少了一次应该是下面的程序
    public  void countMonkeySharePench()
    {
    //最少的时候当然是第五个猴子只取到一个的时候
    int count=1;
    for(int i=1;i<6;i++)
    {
    count=count*5+1;
    System.out.println(count);
    }
    System.out.println("all the pench is :"+count);
    }
      

  8.   

    上面发的又错了,没有看清楚题目的意思,没有将猴子拿走的部分考虑在内,应该是:
    public void countMonkeySharePeach( )
    {
    int i=0, m, j=0, k, count;
    for ( i = 4; i < 10000; i += 4 )
    {
    count = 0;
    m = i;
    for ( k = 0; k < 5; k++ )
    {
    j = i / 4 * 5 + 1;
    i = j;
    if ( j % 4 == 0 ) 
    {
    count++;
    //System.out.println(j);
    }
    else
    break;
    }
    i = m;
    if ( count == 4 )
    {
    System.out.println (j);
    break;
    }
    }
    }
      

  9.   

    这道题应该从后往前推。题目有终结条件的。就是要满足存在这样1个5个数字的集合:{Xi|1=<i<=5},使得总能使4Xi+1=5Xi +1成立。就不写代码了。最后一次第5个猴子分6个(即X1=1)的这个假设肯定是错误。
      

  10.   

    3121以下没有调试!
    class Monkey{
    public static void main(String[] args){
    int count = 6;
            while (count++){

    boolean flag = true;
    int temp = count;
    for(int j=1;j<=5;j++){ if( (temp -1 ) % 5 == 0){
    temp = ((temp - 1)/5) * 4;
    }
    else{
    flag = false;
    break;
    }
    }
    if(flag) {
    System.out.println(count);
    break;
    }
    }


    }
    }
      

  11.   

    怎么格式会乱!再重新编辑以下没有调试! 
    class Monkey{ 
      public static void main(String[] args){ 
      int count = 6; 
            while (count++){            boolean flag = true; 
               int temp = count; 
               for(int j=1;j <=5;j++){                if( (temp -1 ) % 5 == 0){ 
                       temp = ((temp - 1)/5) * 4; 
                   } 
                   else{ 
                       flag = false; 
                       break; 
                   } 
               } 
               if(flag) { 
                   System.out.println(count); 
                   break; 
               } 
            } 
      } 
    }
      

  12.   


    package interview;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int count=1;
    for(int i=1;i<6;i++){
    count=count*5+1;
    }
    System.out.println(count);
    }}count:3906
      

  13.   


    package fzu.test;
    /**
     * 一道笔试题目: 
     *海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,
     *多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩
     *下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿
     *走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有
     *多少个桃子? 
     * @author yeruping
     *
     */
    public class Peaker {
    /*
     * 以下方法理解错误,题目的意思是一只猴子拿走5份中的一份时,剩下4份由下一只猴子再分
     * 而下面的方法是其中的一份再分!理解错误
     */ public void count(){
    int sum =0;//一共有几多
            int[] per = new int[5];//每只猴子分的桃子数
            per[4] = 1;//要求最少的桃子数,既最后一只猴子分到1只桃子
            for(int i=3;i>=0;i--){
                per[i]=per[i+1]*5+1;
            }
            sum = per[0]*5+1;
            System.out.println("每只分别至少分到:"+per[0]+" "+per[1]+" "+per[2]+" "+per[3]+" "+per[4]+"桃子 至少总共:"+sum);
        }public void countRight(){
    /**
     * 直接思路开始算,都符合条件后跳出循环
     * 借用 vampireallen 的  呵呵
     * 不过取消了小于4000同样可以进行的
     */
            int sum = 0;
            int[] per = new int[5]; //每只猴子分的桃子数
             boolean flag = false;
            for (int i = 0; ; i++) {//从0开始向上增长,到后面flag真时跳出!
                sum = i;
                int j = 0;
                for (; j < 5; j++) {
                    if ((sum - 1) % 5 != 0) {
                        break;
                    }
                    sum = (sum - 1) * 4 / 5;
                    per[j]=sum;
                    if (j == 4) {
                        flag = true;
                    }
                }
                if (flag == true) {
                    System.out.println("每只分别至少分到:"+per[0]+" "+per[1]+" "+per[2]+" "+per[3]+" "+per[4]+"桃子 至少总共:"+i);
                    break;
                }
    借用 vampireallen 的
    顺便解释我6楼那个错误;
      

  14.   

    调试通过,想了好久啊,呵呵,public class MonkeyPeach
    {
    public static int peachnum=6; //任意设置一个桃子的初始数量,用以递归。
    public static int monkeynum=5; //猴子总数,可以任意设置猴子数量。
    public static void main(String[] args)
    {
    peach(peachnum,monkeynum);
    }
    public static void peach(int num,int monkey)
    {
    if(monkey==0)
    {
    System.out.println("总桃子数为:"+num);
    System.exit(0);
    }
    if(num%4!=0)
    {
    do
    {
    peachnum++;
    }
    while(peachnum%4!=0);
    peach(peachnum,monkeynum);
    }
    peach(num/4*5+1,--monkey);
    }
    }
      

  15.   

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package peach;/**
     *
     * @author Administrator
     */
    public class Main {
       
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            //第5只猴子拿到得桃子
             int count=0;
                   
            for(int x=1;x<10000;x++){
                 boolean isInt=false;
                 count=5*x+1;//第5堆得桃子数量
                for(int i=0;i<4;i++){                
                    if (count%4==0) {                   
                    count=count*5/4+1;
                    isInt=true;
                    }else isInt=false;
                }
                 if(isInt)
                 {System.out.print(count+"\t");
                     System.out.println(x);}
            }
            
        }}
      

  16.   


    (3901-1)/5=780
    (780*4-1)/5=??????虽然是java初学,但是这么明显的结果,中学生的水平也能看出来啊,不负责任的回帖
      

  17.   

    我3分钟算出来了答案是3906,刚开始算成是781,当然如果按照数学的思维答案是对的,但是会导致第五个猴子的时候只有1个桃子,怎么分成5等份呢?每个猴子是0个,然后刚好剩下一个,但这个不太符合实际,所以我觉得3906是比较准确的结果
    static int ok()
    {
    for(int i=1000;;i++)
    {
    int temp=i;
    for(int j=0;j<5;j++)
    if((i-1)%5==0)
    {
    i=(i-1)/5;
    if(j==4)
    return temp;
    }
    i=temp;
    }
    }
    从1000开始就是为了避免出现781的结果,其实我觉得26楼的朋友的算法是最优化的,他假设第五个猴子的时候剩下6个桃子,这是最好的方法了,我的事枚举,有点耗时。
      

  18.   

    对不起了各位 ,37 ,38楼都是我发的帖子,我以为结果是 对的,不过我看错了题目,因为猴子只是拿走其中的一份,当然代码只要稍微改动一点点就行了
    static int ok()
    {
    for(int i=0;;i++)
    {
    int temp=i;
    for(int j=0;j<5;j++)
    if((i-1)%5==0)
    {
    i=(i-1)/5*4;
    if(j==4)
    return temp;
    }
    i=temp;
    }
    }
    哈哈哈,结果是3121,反正不管怎么说,我的代码是足够简单的,哈哈。