<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

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

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

    java實現(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只螞蟻當前在竿上的位置
    enumDirection枚舉了所有的32種初始化方向,1代表向左,2代表向右

    最短11秒, 最大25秒

    運行結(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 銀河使者 閱讀(6665) 評論(10)  編輯  收藏 所屬分類: javaalgorithm 原創(chuàng)

    評論

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

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

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

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

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

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

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

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

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

    很明顯最小時間為3秒!!!!因為有一只螞蟻在第三CM處,如它一開始就向第0CM處跑,那不就三秒鐘就離開了桿了嗎?而題目是:求所有螞蟻都離開木桿的最小時間!!!!!!!
    2008-07-09 10:48 | jackie

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

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

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

    這道題的若干trick:
    1. 兩只螞蟻相撞后折返,和兩只螞蟻相撞后互相穿過效果是一樣的;
    2. 左右方向的遍歷用bit位更方便:1表示左,0表示右;
    3. 其實因為只需要求出最常最短時間,甚至都不用遍歷所有的方向,參見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實現(xiàn)[未登錄]  回復  更多評論   

    其實想想之后就是一個數(shù)學定律:

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

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

    最長24秒,因為作者寫的檢查條件是 && ants[i] < 28,多寫了一厘米造成。
    思路挺不錯,我的思路是用多線程來解決,這個比我的思路簡單一些。看來高人不少啊!

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

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

    根據(jù)Blues的思路實現(xiàn)
    根本就是一道數(shù)學題嘛
    用不到遍歷,純數(shù)學
    貼代碼:
    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
    主站蜘蛛池模板: 在线观看国产区亚洲一区成人| 久久香蕉国产线看观看亚洲片| yellow免费网站| 亚洲AV日韩AV天堂久久| 色婷婷7777免费视频在线观看 | 亚洲成电影在线观看青青| 大陆一级毛片免费视频观看i| 一级一级毛片免费播放| 亚洲精品美女视频| 免费看男女下面日出水视频| 一级毛片成人免费看免费不卡| 亚洲男人的天堂网站| 久久亚洲精品国产精品黑人| 蜜臀91精品国产免费观看| A片在线免费观看| 亚洲av永久无码精品网址| 亚洲国产成人久久综合一| 国产亚洲福利一区二区免费看| 在线免费观看亚洲| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 国产免费私拍一区二区三区| 两个人看www免费视频| 亚洲精品一卡2卡3卡四卡乱码| 久久精品国产亚洲麻豆| 国产成人免费A在线视频| 9277手机在线视频观看免费| 精品一区二区三区高清免费观看| 在线综合亚洲欧洲综合网站| 亚洲人成依人成综合网| 亚洲中文字幕伊人久久无码| 女人18毛片特级一级免费视频 | 四虎免费久久影院| 国产乱码免费卡1卡二卡3卡| 日本免费中文字幕| 在线观看免费黄色网址| 羞羞视频在线免费观看| 亚洲色中文字幕在线播放| 亚洲欧洲日产韩国在线| 久久精品亚洲综合| 亚洲精品无码专区在线在线播放| 免费人成网站7777视频|