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 閱讀(493) 評論(0)  編輯  收藏 所屬分類: java邏輯題目
          主站蜘蛛池模板: 临武县| 西乌珠穆沁旗| 肃宁县| 呼玛县| 平南县| 房产| 民丰县| 南华县| 临高县| 铜梁县| 金湖县| 桓台县| 交城县| 上思县| 浮梁县| 曲麻莱县| 社旗县| 白水县| 山东省| 元谋县| 遵化市| 铜陵市| 拉萨市| 奎屯市| 巴林左旗| 古蔺县| 剑川县| 黄龙县| 宝鸡市| 鸡泽县| 拜泉县| 万安县| 漳平市| 铜陵市| 昔阳县| 兰西县| 琼中| 古蔺县| 安吉县| 苏尼特左旗| 阳朔县|