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.

Below is the syntax highlighted version of Rational.java from §9.2 Symbolic Methods. 摘自http://introcs.cs.princeton.edu/java/92symbolic/Rational.java.html

 /*************************************************************************
* Compilation: javac Rational.java
* Execution: java Rational
*
* Immutable ADT for Rational numbers.
*
* Invariants
* -----------
* - gcd(num, den) = 1, i.e, the rational number is in reduced form
* - den >= 1, the denominator is always a positive integer
* - 0/1 is the unique representation of 0
*
* We employ some tricks to stave of overflow, but if you
* need arbitrary precision rationals, use BigRational.java.
*
*************************************************************************/ public class Rational implements Comparable<Rational> {
private static Rational zero = new Rational(0, 1); private int num; // the numerator
private int den; // the denominator // create and initialize a new Rational object
public Rational(int numerator, int denominator) { // deal with x/0
//if (denominator == 0) {
// throw new RuntimeException("Denominator is zero");
//} // reduce fraction
int g = gcd(numerator, denominator);
num = numerator / g;
den = denominator / g; // only needed for negative numbers
if (den < 0) { den = -den; num = -num; }
} // return the numerator and denominator of (this)
public int numerator() { return num; }
public int denominator() { return den; } // return double precision representation of (this)
public double toDouble() {
return (double) num / den;
} // return string representation of (this)
public String toString() {
if (den == 1) return num + "";
else return num + "/" + den;
} // return { -1, 0, +1 } if a < b, a = b, or a > b
public int compareTo(Rational b) {
Rational a = this;
int lhs = a.num * b.den;
int rhs = a.den * b.num;
if (lhs < rhs) return -1;
if (lhs > rhs) return +1;
return 0;
} // is this Rational object equal to y?
public boolean equals(Object y) {
if (y == null) return false;
if (y.getClass() != this.getClass()) return false;
Rational b = (Rational) y;
return compareTo(b) == 0;
} // hashCode consistent with equals() and compareTo()
public int hashCode() {
return this.toString().hashCode();
} // create and return a new rational (r.num + s.num) / (r.den + s.den)
public static Rational mediant(Rational r, Rational s) {
return new Rational(r.num + s.num, r.den + s.den);
} // return gcd(|m|, |n|)
private static int gcd(int m, int n) {
if (m < 0) m = -m;
if (n < 0) n = -n;
if (0 == n) return m;
else return gcd(n, m % n);
} // return lcm(|m|, |n|)
private static int lcm(int m, int n) {
if (m < 0) m = -m;
if (n < 0) n = -n;
return m * (n / gcd(m, n)); // parentheses important to avoid overflow
} // return a * b, staving off overflow as much as possible by cross-cancellation
public Rational times(Rational b) {
Rational a = this; // reduce p1/q2 and p2/q1, then multiply, where a = p1/q1 and b = p2/q2
Rational c = new Rational(a.num, b.den);
Rational d = new Rational(b.num, a.den);
return new Rational(c.num * d.num, c.den * d.den);
} // return a + b, staving off overflow
public Rational plus(Rational b) {
Rational a = this; // special cases
if (a.compareTo(zero) == 0) return b;
if (b.compareTo(zero) == 0) return a; // Find gcd of numerators and denominators
int f = gcd(a.num, b.num);
int g = gcd(a.den, b.den); // add cross-product terms for numerator
Rational s = new Rational((a.num / f) * (b.den / g) + (b.num / f) * (a.den / g),
lcm(a.den, b.den)); // multiply back in
s.num *= f;
return s;
} // return -a
public Rational negate() {
return new Rational(-num, den);
} // return a - b
public Rational minus(Rational b) {
Rational a = this;
return a.plus(b.negate());
} public Rational reciprocal() { return new Rational(den, num); } // return a / b
public Rational divides(Rational b) {
Rational a = this;
return a.times(b.reciprocal());
} // test client
public static void main(String[] args) {
Rational x, y, z; // 1/2 + 1/3 = 5/6
x = new Rational(1, 2);
y = new Rational(1, 3);
z = x.plus(y);
System.out.println(z); // 8/9 + 1/9 = 1
x = new Rational(8, 9);
y = new Rational(1, 9);
z = x.plus(y);
System.out.println(z); // 1/200000000 + 1/300000000 = 1/120000000
x = new Rational(1, 200000000);
y = new Rational(1, 300000000);
z = x.plus(y);
System.out.println(z); // 1073741789/20 + 1073741789/30 = 1073741789/12
x = new Rational(1073741789, 20);
y = new Rational(1073741789, 30);
z = x.plus(y);
System.out.println(z); // 4/17 * 17/4 = 1
x = new Rational(4, 17);
y = new Rational(17, 4);
z = x.times(y);
System.out.println(z); // 3037141/3247033 * 3037547/3246599 = 841/961
x = new Rational(3037141, 3247033);
y = new Rational(3037547, 3246599);
z = x.times(y);
System.out.println(z); // 1/6 - -4/-8 = -1/3
x = new Rational( 1, 6);
y = new Rational(-4, -8);
z = x.minus(y);
System.out.println(z);
} }

Twitter OA prepare: Rational Sum的更多相关文章

  1. Twitter OA prepare: even sum pairs

    思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2

  2. Twitter OA prepare: Two Operations

    准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...

  3. Twitter OA prepare: Equilibrium index of an array

    Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...

  4. Twitter OA prepare: K-complementary pair

    2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...

  5. Twitter OA prepare: Anagram is A Palindrome

    Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...

  6. Twitter OA prepare: Visit element of the array

    分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...

  7. Twitter OA prepare: Flipping a bit

    You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one ...

  8. PAT1081:Rational Sum

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

  9. PAT 1081 Rational Sum

    1081 Rational Sum (20 分)   Given N rational numbers in the form numerator/denominator, you are suppo ...

随机推荐

  1. soanr - 企业用户角色管理

    首先sonar支持群组 即 支持企业角色权限管理,其次sonar支持单项目用户权限管理 即 外包,客户,外编人员用户权限管理. (视图内可看到源码) 按照 管路员.产品/项目管理.产品/项目开发.外包 ...

  2. TX大手笔做业务必然失败的原因

    首先说一个伪命题: 物体会向下落这是一个基本的定律,一个小小的物理规则会覆盖所有物体的行为准则. 那么,当地球上的所有东西都下落的时候,你指望整个地球,月球,太阳也会下落么? 事实上大家都知道星球在宇 ...

  3. Excel 2007表格内输入http取消自动加上超链接的功能

    经常使用Excel表格工作的也许会发现,当我们在表格内输入http://XXXX时,默认情况下都会自动加上超链接,如下: 当我们点击域名准备编辑修改时,往往都会调用浏览器转到该域名之下,达不到编辑修改 ...

  4. 【CF809D】Hitchhiking in the Baltic States Splay

    [CF809D]Hitchhiking in the Baltic States 题意:给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足$t_1< ...

  5. ios三张图片组合一张

    - (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 { UIGraphicsBeginImageContext(imag ...

  6. Office2010安装需要MSXML版本6.10.1129.0的方法

    今天给朋友装Office2010,由于朋友之前使用的是绿化版的0ffice2007,所以卸载后安装Office遇到了若要安装Office2010,需要在计算机上安装MSXML版本6.10.1129.0 ...

  7. Android 国内集成使用谷歌地图

    extends:http://blog.csdn.net/qduningning/article/details/44778751 由于众做周知的原因在国内使用谷歌地图不太方便,在开发中如果直接使用会 ...

  8. webp图片优化

    根据对目前国内浏览器占比与 WebP 的兼容性分析,大约有 50% 以上的国内用户可以直接体验到 WebP,如果你的网站以图片为主,或者你的产品基于 Chromium 内核,建议体验尝试.假如你打算在 ...

  9. FTP的两种主动模式和被动模式

    参考:https://blog.csdn.net/xqhrs232/article/details/54633006 https://blog.csdn.net/yuanhangq220/articl ...

  10. Python之re模块正则表达式

    re模块用于对python的正则表达式的操作 字符: .匹配除换行符以外的任意字符 \w匹配字母或数字或下划线或汉字 \s匹配任意空白符 \b匹配单词的开始或结束 ^匹配字符串的开始 $匹配字符串的结 ...