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

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

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

    Fantasy's World

    世界的小世界,我的大世界^_^

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      6 Posts :: 0 Stories :: 16 Comments :: 0 Trackbacks

    Problem Statement

     

        

    When a stone is thrown across water, sometimes it will land on the water and bounce rather than falling in right away. Suppose that a stone is thrown a distance of n. On each successive bounce it will travel half the distance as the previous bounce (rounded down to the nearest integer). When it can not travel any further, it falls into the water. If, at any point, the stone lands on an obstruction rather than water, it will not bounce, but will simply deflect and fall into the water. Please look at the figure for further clarification (with black, red and green cells representing banks, obstructions and free water respectively). So, if the stone is thrown a distance 7, it will bounce and travel a distance of 3, then finally a distance of 1, having travelled a total distance of 11 (the green path in the figure). If a stone is thrown a distance of 8, it will reach the opposite bank, and if thrown at distances of 2 or 6 it will hit an obstruction during its travel. These are the three red paths in the figure.



    You are given a String water. An 'X' represents an obstruction, while a '.' represents water free from obstruction. You are to return an int representing the maximum distance a stone can travel and finally fall in the water, without hitting any obstructions, and without reaching the opposite bank (going beyond the end of the string). You may choose any initial distance for the throw, which starts from the left side of the string. A distance of 1 is the first character of the string, etc. If no initial throw will result in the stone landing in the water without hitting an obstruction, return 0.

    Definition

        

    Class:

    SkipStones

    Method:

    maxDistance

    Parameters:

    String

    Returns:

    int

    Method signature:

    int maxDistance(String water)

    (be sure your method is public)

        

     

     

     

    Notes

    -

    Obstructions are at water level, so the stone will not hit any obstructions while it's in the air.

    Constraints

    -

    water will contain between 1 and 50 elements, inclusive.

    -

    Each element of water will contain between 1 and 50 characters, inclusive.

    -

    Each character of each element of water will be 'X' or '.'.

    Examples

    0)

     

        

    "..X.....X..."

    Returns: 11

    This is the example from the problem statement.

    1)

     

        

    "...X..."

    Returns: 3

    If it weren't for the obstruction, we could start with a throw of distance 4, and go a total of 7. But, the best we can do is to throw the stone a distance of 2, and have it skip a distance of 1.

    2)

     

        

    "....X....X...XXXX.X....."

    Returns: 22

    12 + 6 + 3 + 1 = 22, is the best case.

    3)

     

        

    "XXXXXXX.XXX.X.."

    Returns: 15

    Here, an initial throw of 8 is the only way to avoid hitting an obstruction. Notice that the stone finally falls in the water just before reaching the opposite bank.

     

     


    這次的題目可以說并不是太難,也許很多人被全英文的題目給難住了,其實并不應該。像我這個還沒過CET4的人都能看得懂,何況是大家呢:)好了,廢話不多說了,下面是我寫的答案:

    public class SkipStones
    {
     public int sum;
     public int total;
     public int maxDistance(String water)
     {
      for(int i=water.length();i>0;i--)
      {
       total=0;
       sum=0;
       int j=i;
       do
       {
        sum+=j;
        j/=2;
        }while(j!=0);
       if(sum>water.length()) continue;
       else
       {
        j=i;
        int b=j-1;
        while(j!=0)
        {
         if(water.charAt(b)=='X') break;
         else
         {
          total+=j;
          j/=2;
          b+=j;
          }
         }
        }
        if(total==sum) break;
       }
       if(total==sum) return sum;
       else return 0;
      }
      
      
     public static void main(String[] args)
     {
      SkipStones a=new SkipStones();
      System.out.println("The maxdistance is "+a.maxDistance("..X.....X..."));
      }
     }
    posted on 2005-12-14 16:32 FinalFantasy 閱讀(1670) 評論(5)  編輯  收藏 所屬分類: 新聞

    Feedback

    # re: Google編程挑戰賽250分題目及答案 2005-12-14 16:48 吳淦
    聽你的口氣,難道樓主冠軍勢在必得?  回復  更多評論
      

    # re: Google編程挑戰賽250分題目及答案 2005-12-14 19:07 luffy520
    嗯...加油  回復  更多評論
      

    # re: Google編程挑戰賽250分題目及答案 2005-12-15 11:28 FinalFantasy
    呵呵,很不巧的是比賽那天我發燒了,進去只把題目給拷了出來:(如果那天我沒生病的話,我想通過入圍賽是不成問題的,不過其它的就不敢說了,畢竟誰也不知道google的下一步會是怎樣的:)  回復  更多評論
      

    # re: Google編程挑戰賽250分題目及答案 2005-12-15 13:32 Spirit
    一種遞歸解法:

    public class SkipStones {
    private String water = "...X...";

    public int maxDistance(String water) {
    this.water = water;
    int max = 0;
    int sum = 0;
    for (int initial = 1; initial < water.length() + 1; initial++) {
    sum = bounce(0, initial);
    max = (sum > max ? sum : max);
    }
    return max;
    }

    private int bounce(int startDistance, int bounceDistance) {
    if (bounceDistance == 0)
    return startDistance;

    if ((startDistance + bounceDistance) > water.length())
    return -1;

    if (water.charAt(startDistance + bounceDistance - 1) == 'X')
    return startDistance;

    return bounce(startDistance + bounceDistance, bounceDistance / 2);
    }

    public static void main(String[] args) {
    SkipStones skipStones = new SkipStones();
    System.out.println(skipStones.maxDistance(args[0]));
    }
    }
      回復  更多評論
      

    # re: Google編程挑戰賽250分題目及答案 2005-12-15 16:33 FinalFantasy
    呵呵,謝謝Spirit了。其實剛開始的時候我也是想到用遞歸的,但是覺得用這種方法好理解一些,所以就造這種思路寫出來了..........  回復  更多評論
      


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲高清视频一视频二视频三| 国产啪精品视频网免费| 免费va在线观看| 国产午夜亚洲精品不卡免下载 | a级在线免费观看| 亚洲国产成人久久一区WWW| 美美女高清毛片视频黄的一免费 | 精品亚洲一区二区三区在线播放| 国产亚洲高清在线精品不卡| 又粗又硬免费毛片| 一级毛片不卡免费看老司机| 中文字幕亚洲一区| 麻豆精品不卡国产免费看| 久久久综合亚洲色一区二区三区| 久久久国产精品福利免费| 日韩亚洲AV无码一区二区不卡 | 亚洲av无码专区国产不乱码| 日本高清免费网站| aa在线免费观看| 久久久久亚洲AV无码专区首| 99久久免费观看| wwwxxx亚洲| 免费一区二区三区四区五区| 一级毛片a女人刺激视频免费| 久久精品国产96精品亚洲| 最近免费字幕中文大全视频| 国产精品亚洲一区二区麻豆| 国产一级淫片免费播放电影| 一个人免费视频观看在线www| 亚洲精品不卡视频| 国产免费131美女视频| 岛国精品一区免费视频在线观看 | 久久亚洲精品成人777大小说| 免费观看的毛片大全| 亚洲精品国产精品| 中文字幕在线亚洲精品| 国产精品视频免费观看| 男女猛烈xx00免费视频试看| 亚洲日本va午夜中文字幕一区| 日韩在线天堂免费观看| 十八禁视频在线观看免费无码无遮挡骂过|