<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

    Problem Statement

        

    A rational number is defined as a/b, where a and b are integers, and b is greater than 0. Furthermore, a rational number can be written as a decimal that has a group of digits that repeat indefinitely. A common method of writing groups of repeating digits is to place them inside parentheses like 2.85(23) = 2.852323 ... 23...

    Given a decimal representation of a rational number in decimalNumber, convert it to a fraction formatted as "numerator/denominator", where both numerator and denominator are integers. The fraction must be reduced. In other words, the denominator must be as small as possible, but greater than zero.

    Definition

        
    Class: RecurringNumbers
    Method: convertToFraction
    Parameters: String
    Returns: String
    Method signature: String convertToFraction(String decimalNumber)
    (be sure your method is public)
        

    Constraints

    - decimalNumber will have between 3 and 10 characters inclusive.
    - decimalNumber will contain only characters '0' - '9', '.', '(' and ')'.
    - The second character in decimalNumber will always be '.'.
    - There will be at most one '(' and ')' in decimalNumber.
    - '(' in decimalNumber will be followed by one or more digits ('0' - '9'), followed by ')'.
    - ')' in decimalNumber will not be followed by any other character.

    Examples

    0)
        
    "0.(3)"
    Returns: "1/3"
    0.(3) = 0.333... = 1/3
    1)
        
    "1.3125"
    Returns: "21/16"
    Note there are no recurring digits here, although we could write it as 1.3125(0) or 1.3124(9).
    2)
        
    "2.85(23)"
    Returns: "14119/4950"
    2.85(23) = 2.852323... = 285/100 + 23/9900 = 28238/9900 = 14119/4950. Make sure to reduce the fraction, as shown in the final step.
    3)
        
    "9.123(456)"
    Returns: "3038111/333000"
    4)
        
    "0.111(1)"
    Returns: "1/9"
    5)
        
    "3.(000)"
    Returns: "3/1"

    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

    posted on 2005-12-23 09:30 emu 閱讀(1372) 評論(2)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

    評論

    # re: RecurringNumbers (code jam china 1000分真題) 2006-07-07 15:24 黎吾平
    import java.util.*;

    public class RecurringNumbers
    {
    public static void main(String[] args)
    {
    System.out.println(convertToFraction("9.123(456)"));
    }

    public static String convertToFraction(String decimalNumber)
    {
    int intPart = Integer.parseInt(decimalNumber.substring(0, 1)); //整數部分

    int dotIndex = decimalNumber.indexOf('.'); //小數點的位置
    int braceIndex = decimalNumber.indexOf('('); //左括號的位置
    int lastIndex = 0;

    if (braceIndex == -1) //無循環部分
    {
    lastIndex = decimalNumber.length();
    }
    else //有循環部分
    {
    lastIndex = braceIndex;
    }

    //讀取非循環部分
    String part1 = decimalNumber.substring(dotIndex+1, lastIndex);

    int numerator, denominator = 0;
    if (braceIndex != -1) //有循環部分
    {
    //讀循環部分
    lastIndex = decimalNumber.indexOf(')');
    String part2 = decimalNumber.substring(braceIndex+1, lastIndex);


    //生成全是9的數,長度為循環部分的長度
    for (int i = 0; i < part2.length(); i++)
    {
    denominator = denominator * 10 + 9;
    }

    if (part1.length() == 0) //如果非循環部分為空
    {
    numerator = Integer.parseInt(part2) - 0;
    }
    else
    {
    numerator = Integer.parseInt(part1 + part2) - Integer.parseInt(part1);
    denominator *= ((int)Math.pow(10, part1.length()));
    }
    }
    else //無循環部分
    {
    numerator = Integer.parseInt(part1);
    denominator = (int)Math.pow(10, part1.length());
    }

    //加上整數部分并化簡分數
    numerator += denominator * intPart;
    int min = denominator > numerator ? numerator : denominator;
    for (int i = 2; i <= min;)
    {
    if ((numerator % i == 0) && (denominator % i == 0))
    {
    numerator /= i;
    denominator /= i;
    i = 2;
    }
    else
    {
    i++;
    }
    }

    return Integer.toString(numerator) + "/" + Integer.toString(denominator);
    }
    }  回復  更多評論
      

    # re: RecurringNumbers (code jam china 1000分真題) 2011-08-14 11:46 li.stayhere
    題目很全。 這個題目的關鍵是轉換為等比級數的形式(xyz)=xyz/(10的三次方 - 1)  回復  更多評論
      

    主站蜘蛛池模板: 五月天国产成人AV免费观看| 污网站在线免费观看| 日韩精品在线免费观看| 久久亚洲2019中文字幕| 成人嫩草影院免费观看| 亚洲日韩在线观看| 精品免费久久久久国产一区| 亚洲综合区小说区激情区| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 亚洲精品无码永久在线观看| 免费国产黄网站在线看| 亚洲七七久久精品中文国产| 精品无码一级毛片免费视频观看| 国产亚洲无线码一区二区| 久爱免费观看在线网站 | 亚洲一区二区三区在线| 女人毛片a级大学毛片免费| 日韩亚洲人成在线综合| 久久久久亚洲精品中文字幕| 一级特黄aa毛片免费观看| 亚洲精品资源在线| 最新69国产成人精品免费视频动漫| 国产成人亚洲精品电影| 亚洲人成人网站色www| 91精品啪在线观看国产线免费| 亚洲中文字幕人成乱码| 国产自产拍精品视频免费看| 一级毛片视频免费观看| 中文字幕亚洲精品资源网| 麻豆国产入口在线观看免费 | 丰满少妇作爱视频免费观看| 亚洲国产成人久久综合碰碰动漫3d| 四虎在线成人免费网站| 男男gvh肉在线观看免费| 国产精一品亚洲二区在线播放| 精品国产污污免费网站aⅴ | 美女羞羞免费视频网站| 亚洲电影一区二区| 日韩a在线观看免费观看| 亚洲免费人成在线视频观看| 亚洲综合激情五月色一区|