<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
    感覺這種窮舉的辦法,還不如正著來得快  回復  更多評論
      

    主站蜘蛛池模板: 国产精品免费视频一区| 国产福利在线免费| 亚洲成av人片在线观看天堂无码| 亚洲中文字幕久久精品无码2021| 精品无码国产污污污免费网站 | 亚洲欧美日韩中文无线码 | 日本不卡视频免费| 精品久久亚洲一级α| 国产一区二区免费在线| 羞羞视频免费网站含羞草| 啊v在线免费观看| 亚美影视免费在线观看| 亚洲色无码专区在线观看| 免费视频精品一区二区三区 | 亚洲欧美熟妇综合久久久久| 成年男女免费视频网站| 亚洲av无码专区在线观看下载 | 国产日韩亚洲大尺度高清| 人人玩人人添人人澡免费| 亚洲欧洲精品久久| 国产在线观看片a免费观看| 亚洲国产无线乱码在线观看| 亚洲国产成人爱av在线播放| 国产免费一区二区视频| 亚洲乱人伦精品图片| 免费国产a国产片高清网站| 亚美影视免费在线观看| 亚洲视频在线观看地址| 国内自产拍自a免费毛片| 丰满少妇作爱视频免费观看| 亚洲嫩模在线观看| 国产精品国产午夜免费福利看| 国产免费久久精品99久久| 亚洲导航深夜福利| 亚洲免费在线观看| 无码区日韩特区永久免费系列| 国产亚洲日韩在线a不卡| 久久精品亚洲中文字幕无码麻豆| 日韩一级免费视频| 久久99精品视免费看| 美国毛片亚洲社区在线观看|