隨筆 - 312, 文章 - 14, 評論 - 1393, 引用 - 0
          數(shù)據(jù)加載中……

          百度面試題的java實(shí)現(xiàn)

          本文為原創(chuàng),如需轉(zhuǎn)載,請注明作者和出處,謝謝!

              有一根27厘米的細(xì)木桿,在第3厘米、7厘米、11厘米、17厘米、23厘米這五個(gè)位置上各有一只螞蟻。木桿很細(xì),不能同時(shí)通過一只螞蟻。開始時(shí),螞蟻的頭朝左還是朝右是任意的,它們只會朝前走或調(diào)頭,但不會后退。當(dāng)任意兩只螞蟻碰頭時(shí),兩只螞蟻會同時(shí)調(diào)頭朝反方向走。假設(shè)螞蟻們每秒鐘可以走一厘米的距離。編寫程序,求所有螞蟻都離開木桿的最小時(shí)間和最大時(shí)間。

          java實(shí)現(xiàn)代碼

          public class test_ant
          {
              
          private int[] ants = new int[5];
              
          // 1:左 2:右
              private int enumDirection[][] = new int[32][5];
              
          private void initDirection()
              {
                  
          for (int i = 16, column = 0; i > 0; i = i / 2, column++)
                  {
                      
          int n = 1;
                      
          for (int j = 0; j < 32; j++)
                      {
                          
          if((j / i) % 2 == 0)
                              enumDirection[j][column] 
          = 1;
                          
          else
                              enumDirection[j][column] 
          = 2;
                      }
                  }
              }
              
          private boolean checkAnts()
              {
                  
          for (int i = 0; i < 5; i++)
                  {
                      
          if (ants[i] > 0 && ants[i] < 28)
                          
          return true;
                  }
                  
          return false;
              }
              
          private void changeDirection(int row, int col)
              {
                  
          if (enumDirection[row][col] == 1)
                      enumDirection[row][col] 
          = 2;
                  
          else
                      enumDirection[row][col] 
          = 1;
              }
              
          public void antClimb()
              {
                  initDirection();
                  
          for (int n = 0; n < 32; n++)
                  {
                      
          int seconds = 0;
                      ants[
          0= 3;
                      ants[
          1= 7;
                      ants[
          2= 11;
                      ants[
          3= 17;
                      ants[
          4= 23;
                      
          while (checkAnts())
                      {
                          seconds
          ++;
                          
          for (int i = 0; i < ants.length; i++)
                          {
                              
          if (i < ants.length - 1)
                              {
                                  
          // 螞蟻相遇
                                  if ((ants[i] == ants[i + 1])
                                                  
          && ((enumDirection[n][i] + enumDirection[n][i + 1]) == 3))
                                  {
                                      changeDirection(n, i);
                                      changeDirection(n, i 
          + 1);
                                  }
                              }
                              
          if (enumDirection[n][i] == 1)
                                  ants[i]
          --;
                              
          else
                                  ants[i]
          ++;
                          }
                      }
                      
          for (int j = 0; j < 5; j++)
                          System.out.print(enumDirection[n][j]);
                      System.out.println(
          "");
                      System.out.println(seconds);
                  }
              }
              
          public static void main(String[] args)
              {
                  
          new test_ant().antClimb();
              }
          }


          其中ants數(shù)組保存了5只螞蟻當(dāng)前在竿上的位置
          enumDirection枚舉了所有的32種初始化方向,1代表向左,2代表向右

          最短11秒, 最大25秒

          運(yùn)行結(jié)果

          11111
          23
          11112
          17
          11112
          23
          11122
          11
          11112
          23
          11122
          17
          11122
          23
          11222
          17
          11112
          23
          11122
          21
          11122
          23
          11222
          21
          11122
          23
          11222
          21
          11222
          23
          12222
          21
          11112
          25
          11122
          25
          11122
          25
          11222
          25
          11122
          25
          11222
          25
          11222
          25
          12222
          25
          11122
          25
          11222
          25
          11222
          25
          12222
          25
          11222
          25
          12222
          25
          12222
          25
          22222
          25






          Android開發(fā)完全講義(第2版)(本書版權(quán)已輸出到臺灣)

          http://product.dangdang.com/product.aspx?product_id=22741502



          Android高薪之路:Android程序員面試寶典 http://book.360buy.com/10970314.html


          新浪微博:http://t.sina.com.cn/androidguy   昵稱:李寧_Lining

          posted on 2008-05-10 09:23 銀河使者 閱讀(6673) 評論(10)  編輯  收藏 所屬分類: javaalgorithm 、 原創(chuàng)

          評論

          # re: 百度面試題的java實(shí)現(xiàn)  回復(fù)  更多評論   

          剛剛看到你對這道題目的解釋,新寫了一篇文章,覺得最大是24秒,而且感覺沒有這么復(fù)雜,可以看一下,相互學(xué)習(xí)吧,加油~
          2008-05-10 11:37 | dreamingnest

          # re: 百度面試題的java實(shí)現(xiàn)  回復(fù)  更多評論   

          dreamingnest的思路很好,跳出了"不能同時(shí)通過兩個(gè)螞蟻"的心理暗示.
          2008-05-10 12:42 | jacky-q

          # re: 百度面試題的java實(shí)現(xiàn)[未登錄]  回復(fù)  更多評論   

          其實(shí)文中螞蟻相撞兩螞蟻調(diào)頭
          可以這樣看問題,如果相撞后,桿可以過兩只螞蟻
          螞蟻不調(diào)頭,即擦肩而過,繼續(xù)沿原來方向前進(jìn)(與調(diào)頭是一樣的,只是換了一只螞蟻而已)
          這樣問題就簡化了,呵呵。
          即所有螞蟻中離桿其中一端最遠(yuǎn)的那只螞蟻,即為最長時(shí)間。
          即27-3=24
          呵呵,最短時(shí)間同理可以推出。。。略
          2008-05-12 14:07 | Blues

          # re: 百度面試題的java實(shí)現(xiàn)  回復(fù)  更多評論   

          北大的OJ上有這道題
          2008-05-22 16:26 | stonebow

          # re: 百度面試題的java實(shí)現(xiàn)  回復(fù)  更多評論   

          很明顯最小時(shí)間為3秒!!!!因?yàn)橛幸恢晃浵佋诘谌鼵M處,如它一開始就向第0CM處跑,那不就三秒鐘就離開了桿了嗎?而題目是:求所有螞蟻都離開木桿的最小時(shí)間!!!!!!!
          2008-07-09 10:48 | jackie

          # re: 百度面試題的java實(shí)現(xiàn)  回復(fù)  更多評論   

          有興趣可以探討下!!我QQ
          330620600
          2008-07-09 10:51 | jackie

          # re: 百度面試題的java實(shí)現(xiàn)  回復(fù)  更多評論   

          這道題的若干trick:
          1. 兩只螞蟻相撞后折返,和兩只螞蟻相撞后互相穿過效果是一樣的;
          2. 左右方向的遍歷用bit位更方便:1表示左,0表示右;
          3. 其實(shí)因?yàn)橹恍枰蟪鲎畛W疃虝r(shí)間,甚至都不用遍歷所有的方向,參見minMax1()。
          public class Ant {

          int len = 27;
          int[] pos = {3,7,11,17,23};

          public static void main(String[] args) {
          new Ant().minMax2();
          }
          public void minMax() {
          int maxs = (1 << 5) - 1;
          int min = Integer.MAX_VALUE;
          int max = Integer.MIN_VALUE;
          int step = 0;
          for(int i = 2; i <= maxs; i++) {
          step = 0;
          for(int j = 0; j < 5; j++) {
          if((i & (1 << j)) != 0) {
          step = Math.max(step,pos[j]);
          } else {
          step = Math.max(step,28 - pos[j]);
          }
          }
          max = Math.max(max,step);
          min = Math.min(min,step);
          }
          System.out.println(min);
          System.out.println(max);
          }

          public void minMax2() {
          int min = Integer.MIN_VALUE;
          int max = Integer.MIN_VALUE;

          for(int i = 0; i < 5; i++) {
          min = Math.max(Math.min(pos[i],28 - pos[i]),min);
          max = Math.max(Math.max(pos[i],28 - pos[i]),max);
          }
          System.out.println(min);
          System.out.println(max);
          }
          }
          2008-09-22 01:38 | geagle

          # re: 百度面試題的java實(shí)現(xiàn)[未登錄]  回復(fù)  更多評論   

          其實(shí)想想之后就是一個(gè)數(shù)學(xué)定律:

          最大時(shí)間:找到離出口最近的螞蟻,然后所有的螞蟻同一個(gè)方向朝反向出去
          最小時(shí)間:找到最靠近中心的螞蟻,然后兩邊的螞蟻朝兩個(gè)方向分別出去
          2009-02-10 14:35 | zero

          # re: 百度面試題的java實(shí)現(xiàn)  回復(fù)  更多評論   

          最長24秒,因?yàn)樽髡邔懙臋z查條件是 && ants[i] < 28,多寫了一厘米造成。
          思路挺不錯(cuò),我的思路是用多線程來解決,這個(gè)比我的思路簡單一些。看來高人不少啊!

          螞蟻不調(diào)頭,即擦肩而過,繼續(xù)沿原來方向前進(jìn)(與調(diào)頭是一樣的,只是換了一只螞蟻而已) ,這個(gè)思路也不錯(cuò),鼓勵(lì)一下!
          2009-12-19 17:23 | 匿名

          # re: 百度面試題的java實(shí)現(xiàn)[未登錄]  回復(fù)  更多評論   

          根據(jù)Blues的思路實(shí)現(xiàn)
          根本就是一道數(shù)學(xué)題嘛
          用不到遍歷,純數(shù)學(xué)
          貼代碼:
          java code:

          public class Test{
          public static int MAX_LENGTH;
          public static void main(String args[]){
          MAX_LENGTH = 27;
          int[] poses = {3,7,11,17,23};
          System.out.println("max:" + max(poses));
          System.out.println("min:" + min(poses));
          }

          public static int max(int[] poses){
          if(poses == null || poses.length == 0){
          System.out.println("oh no,dear, you cann't do that");
          return -1;
          }

          int result1 = poses[0] > MAX_LENGTH - poses[0] ? poses[0] : MAX_LENGTH - poses[0];
          int result2 = poses[poses.length - 1] > MAX_LENGTH - poses[poses.length - 1] ? poses[poses.length - 1] : MAX_LENGTH - poses[poses.length - 1];
          return result1 > result2 ? result1 : result2;
          }
          public static int min(int[] poses){
          if(poses == null || poses.length == 0){
          System.out.println("oh no,dear, you cann't do that");
          return -1;
          }
          int result1 = poses[poses.length / 2] < MAX_LENGTH - poses[poses.length / 2] ? poses[poses.length / 2] : MAX_LENGTH - poses[poses.length / 2];
          if(poses.length % 2 == 0){
          int result2 = poses[poses.length / 2 + 1] < MAX_LENGTH - poses[poses.length / 2 + 1] ? poses[poses.length / 2 + 1] : MAX_LENGTH - poses[poses.length / 2 + 1];
          return result1 > result2 ? result1 : result2;
          }else{
          return result1;
          }
          }
          }
          2010-07-15 11:47 | wavesun
          主站蜘蛛池模板: 富宁县| 鹤庆县| 大兴区| 陆丰市| 政和县| 安平县| 德昌县| 军事| 怀远县| 甘孜县| 普格县| 苍溪县| 宁强县| 曲松县| 东兴市| 洪湖市| 秭归县| 和龙市| 仙游县| 忻州市| 万山特区| 邻水| 沭阳县| 同心县| 招远市| 博客| 资兴市| 满洲里市| 鹤岗市| 江北区| 勃利县| 潜江市| 保定市| 新乡县| 澄江县| 涞水县| 手机| 化德县| 桦南县| 盘山县| 万年县|