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

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

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

    emu in blogjava

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks

    SkipStones 
    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.

    給予一個 String 輸入:water. 其內’X'字符代表礁石,’.’ 代表無礁石水面。你的程序
    返回一個整數int,表示該石頭在最終落入水中的情況下運行的總最大距離,觸礁、撞岸的
    情形都必須排除。其中撞岸表征為 (字符串訪問越上界). 你可以選擇任何初始的投擲距離
    ,出發點為字符串最左側。距離為1則抵達字符串的第一個字符,依此類推。如果沒有投擲
    距離,石頭被當作直接入水,返回量為0。

    定義:

    Class:
    SkipStones
    Method:
    maxDistance
    Parameters:
    String
    Returns:
    int
    Method signature:
    int maxDistance(String water)
    (確保你的函數為公共類型 public)

    注:
    礁石處在水面,所以不存在石頭入水后撞礁的情形。

    限制條件:-
    water 字符串包含1到50個元素[即礁石或水面],1、50包含在內。
    water的每個元素包含1到50個字符, 1、50包含在內。
    water的每個元素的每個字符要么是’X',要么是 ‘.’.

    測試例子

    0)

    “..X…..X…”
    返回: 11
    該例正如題目中(綠色軌跡)所圖示。

    1)

    “…X…”
    返回: 3
    假如沒有該礁石,可以扔一距離4,從而總距離為7。但在有礁石情形下,最佳則是扔出距離2

    ,再跳1而停止。

    2)

    “….X….X…XXXX.X…..”
    返回: 22
    12 + 6 + 3 + 1 = 22, 為最佳結果.

    3)

    “XXXXXXX.XXX.X..”
    返回: 15

    posted on 2005-12-13 13:39 emu 閱讀(3012) 評論(4)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

    評論

    # re: google中國編程挑戰賽資格賽真題 -- SkipStones (朋友轉過來的時候順便給翻譯了) 2005-12-13 14:38 Btw0
    上個星期開始看 Thinking in Java,書還沒看完~~~
    我的解:

    public class SkipStones {
    public int maxDistance(String water) {
    int maxDistance = 0;
    int lastDistance = 0;
    int totalDistance = 0;
    for (int i=0; i<water.length(); i++) {
    if (water.charAt(i) == 'X') {
    maxDistance = 0;
    } else {
    int distance = (i + 1);
    totalDistance = distance;
    while (distance/2 >= 1) {
    distance = (int)distance/2;
    totalDistance += distance;
    if ((totalDistance > water.length()) || (water.charAt(totalDistance-1) == 'X')) {
    maxDistance = 0;
    break;
    } else {
    maxDistance = totalDistance;
    }
    }
    }
    maxDistance = (maxDistance > lastDistance) ? maxDistance : lastDistance;
    lastDistance = maxDistance;
    }
    return maxDistance;
    }
    public static void main(String[] args){
    System.out.println(new SkipStones().maxDistance("..X.....X..."));
    System.out.println(new SkipStones().maxDistance("...X..."));
    System.out.println(new SkipStones().maxDistance("....X....X...XXXX.X....."));
    System.out.println(new SkipStones().maxDistance("XXXXXXX.XXX.X.."));
    }
    }  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- SkipStones (朋友轉過來的時候順便給翻譯了) 2006-07-30 14:00 eyaswoo
    如果倒過來想,從water的最后一個開始,假設是落水點,可以倒跳1,23,4567,就是一個二叉樹,遍歷之,若符合一定條件就跳出,當有一次遍歷可以到達起始岸就成功,這時的n就是這個最遠距離。  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- SkipStones (朋友轉過來的時候順便給翻譯了) 2006-07-30 14:01 eyaswoo
    不知道邏輯有沒有問題啊  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- SkipStones (朋友轉過來的時候順便給翻譯了) 2008-09-22 16:07 hejian
    感覺這種窮舉的辦法,還不如正著來得快  回復  更多評論
      

    主站蜘蛛池模板: 国产在线观看无码免费视频| 亚洲高清视频在线播放| 91精品国产免费入口| 日本亚洲国产一区二区三区| 国产午夜精品理论片免费观看| 亚洲日韩国产精品无码av| 国产精品成人无码免费| 国产午夜免费高清久久影院| 亚洲天堂免费在线| 国产中文在线亚洲精品官网| 香蕉成人免费看片视频app下载| 亚洲爆乳无码精品AAA片蜜桃| 国精无码欧精品亚洲一区| 67194成是人免费无码| 亚洲三级视频在线观看 | 国产在线a不卡免费视频| 99在线视频免费观看| 亚洲色偷偷偷综合网| 亚洲线精品一区二区三区影音先锋| 永久免费毛片在线播放| 三年片免费观看大全国语| 日韩国产欧美亚洲v片| 亚洲精品电影在线| 中文字幕亚洲专区| 日本媚薬痉挛在线观看免费| 免费女人高潮流视频在线观看| 黄色a三级免费看| 亚洲人成网站观看在线播放| 91嫩草免费国产永久入口| 亚洲国产精品综合久久2007 | 亚洲人成未满十八禁网站| 亚洲AV无码一区东京热| 又黄又爽无遮挡免费视频| 0588影视手机免费看片| 免费无码又爽又刺激一高潮| 国产成人亚洲综合a∨| 中文字幕无码精品亚洲资源网久久| 亚洲午夜精品久久久久久人妖| 国产亚洲欧洲Aⅴ综合一区| 国产猛烈高潮尖叫视频免费| 噼里啪啦电影在线观看免费高清 |