Programming contests

DEIK Regionális Programozó Csapatverseny, nyílt kategória, 2019. december 8.

December 8, 2019 10:15 AM – December 8, 2019 3:15 PM

Rational Ratio

Every (positive) rational number can be expressed as a ratio of two (positive) integers. However, in decimal form, rational numbers often have an infinitely repeating pattern, e.g., 1/7 = 0.142857142857142857… A convenient way of writing this repeating pattern is to put a bar over the first occurrence of the repeating part, so 1/7 would be written:


Given a rational number consisting of a series of digits, followed by a decimal point, followed by more digits, and then a number indicating how many of the rightmost digits repeat (i.e., the number of digits under the bar), your task is to find the ratio of two integers, in the most reduced form, that represent the same rational number. For example, for the input “0.142857 6”, you should find 1/7.

Input Specification

The input contains several test cases. Each test case will be a single line with two numbers separated by one space. The first number will consist of 1 to 3 digits, followed by a decimal point, followed by 1 to 11 digits (0–9), representing the decimal form of the number, possibly with leading zeros. The second number will be a positive integer indicating how many of the rightmost digits of the preceding number repeat. The first number will always be greater than 0. The second number will never be less than 1 nor larger than the number of digits to the right of the decimal point.

Output Specification

For each test case, print one line containing the corresponding fraction in its most reduced form, that is, the fraction with the smallest possible integer values in the numerator and denominator.

Sample Input

  1. 0.142857 6
  2. 1.6 1
  3. 123.456 2
download as text file

Output for Sample Input

  1. 1/7
  2. 5/3
  3. 61111/495
download as text file

Original Problem

ICPC North Central North America Regional Programming Contest, 2019

University of Debrecen; Faculty of Informatics; v. 03/01/2019