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. mariadb修改root密码的方法

    mariadb安装好后,root密码为空,可以先使用HeidiSQL链接到数据库,执行以下sql,就可以修改root的密码了 update mysql.user set password=passwo ...

  2. ELK系列七:Elasticsearch的集群配置和监控以及在部署ELK中踩的坑

    1.基本下载安装 #按照ELK系列一博客安装启动即可,没有大坑,注意一下权限即可 chmod -R 777 ./elasticsearch #此外没有java的,注意安装下JDK,我这次部署的环境是C ...

  3. ansible的优化

    Ansible企业实战环境中,如果管理的服务器越来越多,Ansibe执行效率会变得比较慢,可以通过优化Ansible提供工作效率,由于Ansible基于SSH协议通信,SSH连接慢会导致整个基于Ans ...

  4. VC 测试一段程序的运行时间 精确到ms

    分三个步骤 1:声明变量 LARGE_INTEGER litmp; _int64 QPart1,QPart2; double dfMinus,dfFreq, dfTim; QueryPerforman ...

  5. virgo-tomcat-server的生产环境线上配置与管理

    Virgo Tomcat Server简称VTS,VTS是一个应用服务器,它是轻量级, 模块化, 基于OSGi系统.与OSGi紧密结合并且可以开发bundles形式的Spring web apps应用 ...

  6. Linux系统java环境jdk的安装

    在linux环境中jdk的安装有两种方式,一为rpm安装机制,另一种为源码安装(已编译好)因此在ORACLE官网提供两种安装文件,一为rpm格式,另一种为gz格式,两种的安装方式都大同小异的. 1.r ...

  7. python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

    一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...

  8. adviser vs mentor

    研究生或博士生提到自己导师的时候是说adviser呢?还是mentor呢? 至少我认识一个Berkeley的博士是说adviser的. 另外,我的导师也是说adviser. 那还是说adviser吧- ...

  9. spark脚本日志输出级别设置

    import org.apache.log4j.{ Level, Logger } Logger.getLogger("org").setLevel(Level.WARN) Log ...

  10. Mongodb之使用rpm包安装配置启动

    下载rpm包 wget https://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.2/x86_64/RPMS/mongod ...