<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)頭朝反方向走。假設(shè)螞蟻們每秒鐘可以走一厘米的距離。編寫程序,求所有螞蟻都離開木桿的最小時間和最大時間。

    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 銀河使者 閱讀(6671) 評論(10)  編輯  收藏 所屬分類: java 、algorithm 、 原創(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
    主站蜘蛛池模板: 亚洲AV男人的天堂在线观看| 日本道免费精品一区二区| 国产精品福利片免费看| 成人黄色免费网址| 国产亚洲?V无码?V男人的天堂| 亚洲国产综合人成综合网站00| 国产精品免费看久久久香蕉| 亚洲国产天堂在线观看| 美女黄网站人色视频免费| 美女被cao免费看在线看网站| 亚洲国产一二三精品无码| 一级做a爰片久久毛片免费陪| 免费的涩涩视频在线播放| 亚洲精品国产电影午夜| 久久成人免费大片| 亚洲国产aⅴ综合网| 亚洲精品蜜夜内射| 成熟女人牲交片免费观看视频| 久久久久亚洲AV无码麻豆| 久久国产免费一区二区三区| 国产人成亚洲第一网站在线播放| 免费一级肉体全黄毛片| 精品久久久久久亚洲中文字幕| 日韩免费无砖专区2020狼| 亚洲爆乳精品无码一区二区| 亚洲2022国产成人精品无码区| 美女视频黄的免费视频网页 | 最近2019中文字幕mv免费看| 美女被免费网站91色| 丝袜熟女国偷自产中文字幕亚洲| 丝瓜app免费下载网址进入ios | 亚洲第一se情网站| 亚洲精品无码久久久久久久| 国产亚洲精品高清在线| 国产成人免费ā片在线观看| 老司机精品视频免费| ASS亚洲熟妇毛茸茸PICS| 日韩在线天堂免费观看| 亚洲一区二区免费视频| 久久夜色精品国产噜噜亚洲a| 亚洲精品在线观看视频|