Problem Statement |
| | Arational number is defined as a/b, where a and b are integers, and b isgreater than 0. Furthermore, a rational number can be written as adecimal that has a group of digits that repeat indefinitely. A commonmethod of writing groups of repeating digits is to place them insideparentheses 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", whereboth numerator and denominator are integers. The fraction must bereduced. 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) | |
| | |
| 1) | |
| | | | Returns: "21/16" | | Note there are no recurring digits here, although we could write it as 1.3125(0) or 1.3124(9). | | |
| 2) | |
| | | | Returns: "14119/4950" | | 2.85(23)= 2.852323... = 285/100 + 23/9900 = 28238/9900 = 14119/4950. Make sureto reduce the fraction, as shown in the final step. | | |
| 3) | |
| | | | Returns: "3038111/333000" | | | |
| 4) | |
| | |
| 5) | |
| | |
Thisproblem statement is the exclusive and proprietary property ofTopCoder, Inc. Any unauthorized use or reproduction of this informationwithout the prior written consent of TopCoder, Inc. is strictlyprohibited. (c)2003, TopCoder, Inc. All rights reserved.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1287925
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。