Fun with fractions
Time limit1sMemory limit128 MB
Add up to 999 mixed numbers, integers, or fractions per test case and print each sum in lowest terms as a mixed number.
- Level
Easy3 of 10
- Topics
- Math, Implementation, String
- Solved
- No attempts yet
Problem
A rational number is the ratio of two integers. Given a numerator and a denominator , we write the fraction as . A rational number has more than one representation. For example, and have the same value. A representation with is in lowest terms. So is in lowest terms and is not.
Addition of rational numbers is defined below. The right side is not guaranteed to be in lowest terms.
A rational number whose numerator is at least as large as its denominator can be written as a mixed number, which has an integer part and a fractional part. For example, is the mixed number that equals .
Read a sequence of rational numbers and print their sum.
Input
The input holds several tests. The first line of each test has an integer (). A line holding ends the input.
The next lines each hold one string with no spaces. Each string is one rational number in one of the three forms below, and it may not be in lowest terms. , , and are integers with and .
w,n/d: a mixed number equal ton/d: a rational number whose integer part is 0w: an integer whose fractional part is 0
Output
For each test print one line that begins with Test x: , followed by the sum in lowest terms. Here is the test number counting from 1.
If the sum has integer part and fractional part , print it as w,n/d when both parts are present, as w when only the integer part is present, and as n/d when only the fractional part is present. If neither part is present, print 0.