DWITE '10 R2 #4 - Fractions to Decimal

View as PDF

Submit solution

Points: 7
Time limit: 2.0s
Memory limit: 64M

Problem type
DWITE Online Computer Programming Contest, November 2010, Problem 4

In math class, writing fractions in their decimal form was a common task. However, the teachers would usually have you round off the number to a certain number of decimal places. But all fractions have a nice property: in decimal form, they all either terminate or repeat in a pattern. For example, the fraction 1/10 terminates, as 1/10 = 0.1. However, the fraction 1/3 repeats, as 1/3 = {0.333\dots} = 0.(3) (note that the repeated part is parenthesized). Given a fraction, output its decimal form, parenthesizing the cyclic part of the fraction, if such exists.

The input will contain 5 lines, each a pair of integers 1 \le N < D \le 400, separated by a single space.

The output will contain 5 lines, the decimal forms of N/D, truncating and parenthesizing the repeating digits, if such is the case.

Note: the answers must match exactly and use the shortest repeating pattern. Since D is always greater than N, all of the answers are less than 1.0.

Sample Input

1 10
1 3
2 7
2 3
6 10

Sample Output

0.1
0.(3)
0.(285714)
0.(6)
0.6

Problem Resource: DWITE

Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported

Comments

There are no comments at the moment.