Class name: Fractions
Method name: add
Parameters: String[]
Returns: String
For the purpose of this problem we are going to represent fractions in String
objects in the following form: "<numerator>/<denominator>", where <, >, and "
are just for clarity. Some examples of fractions represented as strings are
"1/2", "71/19" and "5/5".
A fraction p/q can be simplifiable if there exists an integer k > 1, which is a
divisor of both p and q. This way the fraction is representable by p1/q1, where
p1 = p/k and q1 = q/k.
Write a class Fractions, which contains a method add. The method should accept
a String[] of fractions, represented in the form above and produce a single
simplified fraction, in the same format, which represents the sum of all
fractions received in the input argument.
The method signature is (Be sure your method is public):
String add (String[] f);
Inputs: (TopCoder will enforce these rules)
* f will contain between 1 and 50, inclusive, elements
* both numerators and denominators will be between 1 and 1000000000, inclusive
* the simplified sum of the first k elements of f (0 < k < f.size()) will
always have its numerator and denominator be between 1 and 1000000000, inclusive
Examples:
f = [1/3, 2/3]
result: 1/1
f = [2/2, 2/4, 2/8, 2/71]
result: 505/284
f = [333/10000000, 333/100000000]
result: 3663/100000000
f = [999999998/999999999, 4/999999999]
result: 333333334/333333333
f = [1/2]
result: 1/2
Examples of invalid input:
f = [1/999999999 1/2 1/999999999]
TopCoder returns: Sum of every prefix's numerator and denominator should be
between 1 and 1000000000 inclusive!
f = [1000000000/13, 2/13]
TopCoder returns: Sum of every prefix's numerator and denominator should be
between 1 and 1000000000 inclusive!
f = [1/100000000000]
TopCoder returns: Both numerators and denominators should be between 1 and
1000000000 inclusive!