DWITE Online Computer Programming Contest

Fractions to Decimal

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... = 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 file DATA4.txt will contain 5 lines, each a pair of integers 1 ≤ N < D ≤ 400, separated by a single space.

The output file OUT4.txt 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