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

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

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

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
    數據加載中……

    關于螞蟻問題(Ants)

    原文地址:
    http://www.tkk7.com/dreamingnest/archive/2008/05/10/199672.html

    之前看有的朋友談百度的一道面試試題-螞蟻問題(有一根27厘米的細木桿,在第3厘米、7厘米、11厘米、17厘米、23厘米這五個位置上各有一只螞蟻。木桿很細,不能同時通過一只螞蟻。開始時,螞蟻的頭朝左還是朝右是任意的,它們只會朝前走或調頭,但不會后退。當任意兩只螞蟻碰頭時,兩只螞蟻會同時調頭朝反方向走。假設螞蟻們每秒鐘可以走一厘米的距離。編寫程序,求所有螞蟻都離開木桿的最小時間和最大時間)。關于這道題目,網上給出了很多的解釋,但從整體來看,基本都是用到了等價置換(等量代換)的思想。要求最小時間,即為“最不容易”先到達兩端的螞蟻以最短的時間到達,所以我們只需找到所有螞蟻中間的一只(共奇數只螞蟻)或兩只(共偶數只螞蟻)到達一端的最短時間。比較麻煩的是求最長時間,有人會覺得當有很多只螞蟻時,中間的螞蟻們相互碰撞的次數多些會增加時間,感覺上比較復雜,可如果我們用等量代換的思想來解釋就比較容易。假設中間的任意兩只相鄰螞蟻即將發生碰撞,如:A ->        <-B,當A,B發生碰撞后,便有<-A    B->。A,B反向相當于<-B   A -> ,即二者繼續向著原來的方向前進,對于任意相鄰的發生碰撞的螞蟻都適用,所以只需求最兩端的兩只螞蟻距離兩端的最遠距離。由以上分析可知,如果出這樣的問題,我們可以不用通過程序便能說出結果:5個點,中間螞蟻的位置為11,即0-11-27,顯然最小為11,最兩端螞蟻,0-3-27,最大為24,0-23-27,最大為23,所以最大為24。對于這個題,給出如下Java代碼(隨便寫了幾句,不符合面向對象思想)。
    public class Ant {
        
    public static void main(String[] args){
            
    int length=27,points=5,min=0,max=0,temp_min=0,temp_max=0;
            
    int[] pos={3,7,11,17,23};
            
    for(int i: pos){
                temp_min
    =i>length-i?length-i:i;
                temp_max
    =i<length-i?length-i:i;
                
    if(temp_min>min)
                    min
    =temp_min;
                
    if(temp_max>max)
                    max
    =temp_max;
            }

            System.out.println(
    "最短時間:"+min+"  最長時間:"+max);
        }

    }
    有了如上的想法,我們能做出判斷,為什么還要寫代碼呢?其實這個問題出自Waterloo Local Contest Sep.19,2004 準確描述如下:
    An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole. 
    The first line of input contains one integer giving the number of cases that follow. The data 
    for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace. 

    For each 
    case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time. 

    Sample Input
    2
    10 3
    2 6 7
    214 7
    11 12 7 13 176 23 191

    Sample Output
    4 8
    38 207

    在這里給出相應的c++代碼:
    #include<iostream>
    using namespace std;
    int main()
    {
        
    int cases,l,n,min,max,temp_min,temp_max,pos;
        cin
    >>cases;
        
    while(cases--)
        
    {
            cin
    >>l>>n;
            min
    =0;
            max
    =0;
            
    while(n--)
            
    {
                cin
    >>pos;
                temp_min
    =pos>l-pos?l-pos:pos;
                temp_max
    =pos<l-pos?l-pos:pos;
                
    if(temp_min>min)
                    min
    =temp_min;
                
    if(temp_max>max)
                    max
    =temp_max;
            }

            cout
    <<min<<' '<<max<<endl;
        }

        
    return 0;
    }


    posted on 2008-05-12 09:07 々上善若水々 閱讀(1458) 評論(0)  編輯  收藏 所屬分類: 數據結構與算法

    主站蜘蛛池模板: 亚洲欧美自偷自拍另类视| 成人免费无码大片a毛片| 久久久亚洲精华液精华液精华液 | 噜噜噜亚洲色成人网站∨| 亚洲精品国产成人影院| 成人毛片免费观看视频大全| 久久成人免费大片| 福利免费在线观看| 国产亚洲精品美女2020久久| 亚洲最大中文字幕无码网站 | 亚洲精品免费观看| 久久久久国色AV免费观看| 国产精品亚洲精品爽爽| 亚洲欧美日韩中文高清www777| 久久久婷婷五月亚洲97号色| 亚洲国产精品成人久久| 亚洲毛片网址在线观看中文字幕| 日本无吗免费一二区| 成全影视免费观看大全二| 午夜福利不卡片在线播放免费| 96免费精品视频在线观看| 日本免费中文字幕| 日本免费高清视频| 免费无码H肉动漫在线观看麻豆 | 久久亚洲综合色一区二区三区| 亚洲中文无韩国r级电影| 无码欧精品亚洲日韩一区夜夜嗨 | 麻豆最新国产剧情AV原创免费| 国产成人精品免费视频网页大全| 无码人妻精品中文字幕免费| 久久青草免费91线频观看站街| 精品在线免费观看| 特级无码毛片免费视频尤物| 日本视频在线观看永久免费| 大地影院MV在线观看视频免费| 成全高清在线观看免费| 一区二区在线免费观看| 2019中文字幕免费电影在线播放| 成人免费的性色视频| 女人张开腿等男人桶免费视频| 在线观看人成网站深夜免费|