Ուղարկել | Բոլոր լուծումները | Լավագույն լուծումները | Վերադառնալ ցուցակին |
ACM_0108 - FUN WITH FRACTIONS |
A rational number can be represented as the ratio of two integers, referred to as the numerator (n) and the denominator (d) and written n/d. A rational number's representation is not unique. For example the rational numbers 1/2 and 2/4 are equivalent. A rational number representation is described as "in lowest terms" if the numerator and denominator have no common factors. Thus 1/2 is in lowest terms but 2/4 is not. A rational number can be reduced to lowest terms by dividing by the greatest common divisor of n and d.
Addition of rational numbers is defined as follows. Note that the right hand side of this equality will not necessarily be in lowest terms.
A rational number for which the numerator is greater than or equal to the denominator can be displayed in mixed format, which includes a whole number part and a fractional part. For example, 51/3 is a mixed format representation of the rational number 16/3.
Your task is to write a program that reads a sequence of rational numbers and displays their sum.
Input
Input will consist of specifications for a series of tests. Information for each test begins with a line containing a single integer 1 <= n < 1000 indicating how many values follow. A count of zero terminates the input.
The n following lines each contain a single string with no embedded whitespace . Each string represents a rational number, which could be in any of the following forms and will not necessarily be in lowest terms (w, n, and d are integers: 0 <= w,n < 1000, 1 <= d
< 1000).
- w,n/d: a mixed number equivalent to the rational number (w*d + n) / d.
- n/d: a rational number with a zero whole number part
- w: a whole number with a zero fractional part
Output
Output should consist of one line for each test comprising the test number (formatted as shown) followed by a single space and the sum of the input number sequence. The sum should be displayed in lowest terms using mixed number format. If either the whole number part or the fractional part is zero, that part should be omitted. As a special case, if both parts are zero, the value should be displayed as a single 0.
Examples
№ |
stdin |
stdout |
1 |
2 1/2 1/3 3 1/3 2/6 3/9 3 1 2/3 4,5/6 0 |
Test 1: 5/6 Test 2: 1 Test 3: 6,1/2 |
Ավելացրեց. | Հրանտ Հովհաննիսյան |
Ամսաթիվ. | 2014-01-29 |
Ժամանակի սահմանափակումը. | 1s |
Ծրագրի տեքստի սահմանափակումը. | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Լեզուներ. | Բոլորը բացի ASM32 ASM64 GAWK CLPS CLOJURE D ERL FSHARP FORTRAN GOSU HASK ICON ICK NEM NIM OBJC-CLANG PICO PIKE PYPY PYPY3 PY_NBC RUST SCM guile CHICKEN SED TCL WHITESPACE |
Աղբյուրը. | Australian Programming Contest 2013.C |