Twitter OA prepare: Rational Sum - neverlandly - 博客园
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q , where p & q are two integers, and the denominator q is not equal to zero. Hence, all integers are rational numbers where denominator, in the most reduced form, is equal to 1. You are given a list of N rational number, {a1/b1, a2/b2, ..., aN/bN}. Print the sum ( = a1/b1 + a2/b2 + ... + aN/bN = num/den) in the most reduced form. Input The first line of input contains an integer, N, the number of rational numbers. N lines follow. ithline contains two space separated integers, ai bi, where aiis the numerator and bi is the denominator for the ith rational number. Output You have to print two space separated integers, num den, where num and den are numerator and denominator of the sum respectively. Constraints 1 <= N <= 15 1 <= ai <= 10 1 <= bi <= 10 Notes Make sure the sum displayed as output is in the most reduced form. If sum is an integer, you have to print 1 as denominator. Sample Input 4 4 2 2 4 2 4 2 3 Sample Output 11 3 Explanation Sum is 4/2 + 2/4 + 2/4 + 2/3 = (24 + 6 + 6 + 8)/12 = 44/12 = 11/3. So you have to print "11 3", which is the most reduced form.Read full article from Twitter OA prepare: Rational Sum - neverlandly - 博客园
No comments:
Post a Comment