HelloWorld 善战者,求之于势,不责于人;故能择人而任势。

知止而后有定,定而后能静,静而后能安,安而后能虑,虑而后能得。物有本末,事有终始。知所先后,则近道矣。

  BlogJava :: 首页 ::  :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 40 评论 :: 0 Trackbacks
问题:有 12 颗玻璃球大小一样, 不知道哪一颗重了, 还是轻了. 请用天平秤秤3次, 把其中的一颗重量不均匀的玻璃球取出来!


class Ball {
     private int weight;
   
     public void setWeight(int weight) {
         this.weight = weight;
     }
   
     public int getWeight() {
         return this.weight;
     }
}

class ComputerDifBall {
     /**
      * 天平称方法
      * @param balls1
      * @param balls2
      * @return int 0:平,1:balls1重,2:balls2重
      */
     public int balance(Ball[] balls1, Ball[] balls2) {
         for (int i = 0; i < balls1.length; i++) {
             int weight1 = balls1
.getWeight();
             int weight2 = balls2
.getWeight();
             if (weight1 > weight2) {
                 return 1;
             } else if (weight1 < weight2) {
                 return 2;
             }
         }
         return 0;
     }
   
     /**
      * 天平称方法
      * @param balls1
      * @param balls2
      * @return int 0:平,1:balls1重,2:balls2重
      */    
     public int balance (Ball ball1, Ball ball2) {
         if (ball1.getWeight() == ball2.getWeight()) {
             return 0;
         } else if (ball1.getWeight() > ball2.getWeight()) {
             return 1;
         } else {
             return 2;
         }
     }
   
     /**
      * 算出哪一个不一样,是重了还是轻了
      * @param balls
      * @return
      */
     public void balanceMethod(Ball[] balls) {
         Ball[] A = {balls[0],balls[1],balls[2],balls[3]};
         Ball[] B = {balls[4],balls[5],balls[6],balls[7]};
         Ball[] C = {balls[8],balls[9],balls[10],balls[11]};
         //1.称第一次
         int first = balance(A,B);
         if (first == 0) {
             //A==B,C组中有问题
             Ball[]D = new Ball[3];
             Ball[]E = new Ball[3];
             for (int i = 0 ; i < 3; i++) {
                 D
= A;
                 E
= C;
             }
             //2.称第二次
             int second = balance(D,E);
             if (second == 0) {
                 //D,E相等,C中第4个有问题
                 //3.称第三次
                 int third = balance(A[0],C[3]);
                 System.out.println("第12个球有问题!"+(third==2?"这个球重了!":"这个球轻了!"));
             } else {
                 //3.称第三次
                 int third = balance(C[0],C[1]);
                 if (third == 0) {
                     System.out.println("第11个球有问题!"+(third==2?"这个球重了!":"这个球轻了!"));
                 } else {
                     if (second == 1) {
                         if (third == 1) {
                             System.out.println("第10个球有问题!这个球轻了!");
                         } else {
                             System.out.println("第9个球有问题!这个球轻了!");
                         }
                     } else {
                         if (third == 1) {
                             System.out.println("第9个球有问题!这个球重了!");
                         } else {
                             System.out.println("第10个球有问题!这个球重了!");
                         }                        
                     }
                 }
             }
         } else {
             Ball[] D = {A[0],A[1],B[0],C[0]};
             Ball[] E = {A[2],A[3],B[1],C[1]};
             //2.称第二次
             int second = balance(D,E);
             if (second == 0) {
                 //A和B不等,且A[0]+A[1]+B[0]+C[0] == A[2]+A[3]+B[1]+C[1]
                 //则肯定是B[2]/B[3]中有个球不一样
                 //3.称第三次
                 int third = balance(B[2],B[3]);
                 if (first == 1) {
                     if (third > 1 ) {
                         System.out.println("第7个球有问题!这个球轻了!");
                     } else {
                         System.out.println("第8个球有问题!这个球轻了!");
                     }                
                 } else {
                     if (third > 1 ) {
                         System.out.println("第8个球有问题!这个球重了!");
                     } else {
                         System.out.println("第7个球有问题!这个球重了!");
                     }    
                 }
             } else {
                 Ball[] F = {A[0],A[3]};
                 Ball[] G = {A[1],A[2]};
                 //3.称第三次
                 int third = balance(F,G);
                 if (third == 0) {
                     //A中的球都是一样,B中有不相同的球
                     //B中B[0],B[1]不一样
                     if (first == 1) {
                         if (second == 1) {
                             System.out.println("第6个球有问题!这个球轻了!");
                         } else {
                             System.out.println("第5个球有问题!这个球轻了!");
                         }
                     } else {
                         if (second == 1) {
                             System.out.println("第5个球有问题!这个球重了!");
                         } else {
                             System.out.println("第6个球有问题!这个球重了!");
                         }                        
                     }
                 } else {
                     if (third == 1) {
                         //则A[0],A[1]中有一个重的球
                         if (first == 1) {
                             if (second == 1) {
                                 System.out.println("第1个球有问题!这个球重了!");
                             } else {
                                 System.out.println("第4个球有问题!这个球重了!");
                             }
                         } else {
                             if (second == 1) {
                                 System.out.println("第3个球有问题!这个球轻了!");
                             } else {
                                 System.out.println("第2个球有问题!这个球轻了!");
                             }                            
                         }
                     } else {
                         if (first == 1) {
                             if (second == 1) {
                                 System.out.println("第2个球有问题!这个球重了!");
                             } else {
                                 System.out.println("第3个球有问题!这个球重了!");
                             }
                         } else {
                             if (second == 1) {
                                 System.out.println("第4个球有问题!这个球轻了!");
                             } else {
                                 System.out.println("第1个球有问题!这个球轻了!");
                             }                            
                         }
                     }
                 }
             }
         }
     }
   
   
     public static void main(String[] args) {
         ComputerDifBall cdb = new ComputerDifBall();
         for (int i = 0; i < 12; i++) {
             Ball[] ball = new Ball[12];
             for (int j = 0; j < 12; j++) {
                 ball[j] = new Ball();
                 if (i != j)
                     ball[j].setWeight(0);
                 else
                     ball[j].setWeight(10);
             }
             System.out.println("-------------第"+i+"次测试,注意重的球为第"+(i+1)+"个!-----------------");
             cdb.balanceMethod(ball);
             System.out.println();
         }
       
         for (int i = 0; i < 12; i++) {
             Ball[] ball = new Ball[12];
             for (int j = 0; j < 12; j++) {
                 ball[j] = new Ball();
                 if (i != j)
                     ball[j].setWeight(10);
                 else
                     ball[j].setWeight(0);
             }
             System.out.println("-------------第"+i+"次测试,注意轻的球为第"+(i+1)+"个!-----------------");
             cdb.balanceMethod(ball);
             System.out.println();
         }
     }
}


上面是用java写的
分3组是可以称出来的
如果分4组,有一种情况是称不出来
A,B,C,D(每组3个)
A=B
B=C
那么只有D中有一个不同的
,这个时候不能确定那个球是重了还是轻了



</script>

posted on 2007-08-13 19:01 helloworld2008 阅读(488) 评论(0)  编辑  收藏 所属分类: java逻辑题目

只有注册用户登录后才能发表评论。


网站导航: