Programming contests

ECN selejtező programozó csapatverseny, 2019. április 17.

April 17, 2019, 10:15 AM – April 17, 2019, 3:15 PM

Long Division

One thing Harry, Ron, and Hermione have learned at Hogwarts is that you must be precise in everything you do. After all, if you wish to transfigure yourself into a lion, it does no good to change into a kittycat, even though they are approximately the same! With this in mind, Harry has gotten pretty fed up with his calculator that only does long division to 8 places! You are to write a program to perform long division with infinite precision.

Input Specification

Each line will consist of two nonnegative decimal integers no greater than 10000, separated by exactly one space, and followed by one or more newline characters. The first is the numerator; the second is the denominator. The last line in the input will feature a denominator of zero. This line is not to be processed; it only signals the end of the input.

Output Specification

The output cases are to appear in the same order in which they appear in the input. Each case should appear as follows: the numerator, a slash, the denominator, a space, the equals sign, another space, and the answer. If the answer contains no remainder, the answer should simply be a nonnegative decimal integer (without a decimal point). If there is a remainder, the answer should appear as the integer portion, a decimal point, and the decimal portion. If the decimal terminates, the decimal portion is simply the string of digits that follow the decimal point. If the decimal repeats, the repeating portion should be enclosed in parentheses. Note that you must enclose the earliest repeating portion in parentheses: 1/11 = 0.(09), not 0.0(90). Terminating decimals are to be printed as terminating decimals: 1/2 = 0.5, not 0.5(0), or 0.4(9), even though these three are mathematically equivalent.

There is a line width of 50; any line greater than 50 characters must be broken into lines each containing 50 characters, excepting the last which will contain however many are left over. Following the answer should be two newline characters, one to terminate the line, and one to skip a line.

Sample Input

  1. 200 50
  2. 36 24
  3. 355 113
  4. 0 0
download as text file

Output for Sample Input

  1. 200/50 = 4
  2. 36/24 = 1.5
  3. 355/113 = 3.(1415929203539823008849557522123893805
  4. 30973451327433628318584070796460176991150442477876
  5. 1061946902654867256637168)
download as text file

Original Problem

NMU Invitational Programming Contest, 2001

University of Debrecen; Faculty of Informatics; v. 09/30/2024