<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
    主站蜘蛛池模板: 亚洲成A∨人片在线观看不卡| 毛片免费观看视频| 亚洲国产成人久久综合区| 亚洲精华国产精华精华液好用 | 久久九九全国免费| 中文字幕无码精品亚洲资源网| 国产天堂亚洲精品| 全部免费毛片在线| 少妇亚洲免费精品| 中文字幕久久亚洲一区| 成人自慰女黄网站免费大全| 国产乱辈通伦影片在线播放亚洲| 一区二区三区AV高清免费波多| 亚洲无码精品浪潮| 国内永久免费crm系统z在线| 亚洲国产精品自在在线观看 | 国产成人亚洲午夜电影| 亚洲AV成人精品日韩一区18p| 人妻仑刮八A级毛片免费看| 日韩亚洲精品福利| 男女拍拍拍免费视频网站| 亚洲av中文无码乱人伦在线咪咕| 久久免费视频99| 亚洲一区精品视频在线| 日日AV拍夜夜添久久免费| 成人免费视频一区二区| 亚洲国产精品VA在线看黑人| 色欲色香天天天综合网站免费 | 亚洲日韩国产欧美一区二区三区 | 成人激情免费视频| 特级毛片免费播放| 亚洲综合av永久无码精品一区二区| 黄色片免费在线观看| 亚洲一级黄色大片| heyzo亚洲精品日韩| 免费人成黄页在线观看日本| 亚洲六月丁香婷婷综合| 免费永久看黄在线观看app| 免费91麻豆精品国产自产在线观看| 亚洲91精品麻豆国产系列在线 | 日韩免费无砖专区2020狼|