Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int". If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:

5
2/5 4/15 1/30 -2/60 8/3

Sample Output 1:

3 1/3

Sample Input 2:

2
4/3 2/3

Sample Output 2:

2

Sample Input 3:

3
1/3 -1/6 1/8

Sample Output 3:

7/24
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct{
long long up, down;
}fra;
long long gcd(long long a, long long b){
a = abs(a);
b = abs(b);
if(b == )
return a;
else return gcd(b, a % b);
}
fra cacul(fra a, fra b){
fra temp;
temp.down = a.down * b.down;
temp.up = a.down * b.up + b.down * a.up;
long long fact = gcd(temp.up, temp.down);
temp.down = temp.down / fact;
temp.up = temp.up / fact;
return temp;
}
int main(){
int N;
fra re = {,}, temp = {, };
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%lld/%lld", &temp.up, &temp.down);
re = cacul(re, temp);
}
if(re.up == ){
printf("");
}else if(abs(re.up) == abs(re.down)){
printf("%lld", re.up / re.down);
}else if(abs(re.up) > abs(re.down)){
if(re.up % re.down == )
printf("%d", re.up / re.down);
else
printf("%lld %lld/%lld", re.up / re.down, re.up % re.down, re.down);
}else{
printf("%lld/%lld", re.up, re.down);
}
cin >> N;
return ;
}

总结:

1、分数运算化简:化简时分子分母同除最大公因数。 输出时考虑:分子为0时直接输出0;分子>=分母时(用绝对值比较,是>=而非>),可以整除则输出整数,否则输出代分数(4/1直接输出4);

2、gcd函数:

int gcd(int a, int b){ 
if(b == )
return a;
else return gcd(b, a % b);
}

A1081. Rational Sum的更多相关文章

  1. PAT甲级——A1081 Rational Sum

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

  2. PAT_A1081#Rational Sum

    Source: PAT A1081 Rational Sum (20 分) Description: Given N rational numbers in the form numerator/de ...

  3. PAT1081:Rational Sum

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

  4. PAT 1081 Rational Sum

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

  5. PAT Rational Sum

    Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...

  6. PAT 1081 Rational Sum[分子求和][比较]

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

  7. pat1081. Rational Sum (20)

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

  8. 1081. Rational Sum (20) -最大公约数

    题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...

  9. Twitter OA prepare: Rational Sum

    In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...

随机推荐

  1. RuntimeError: Python is not installed as a framework 错误解决方案

    在virtualenv环境下使用matplotlib绘图时遇到了这样的问题: >>> import matplotlib.pyplot as pltTraceback (most r ...

  2. GeForce Experience关闭自动更新

    GeForce Experience驱动更新很烦,而且有时更新后就打不开了,找到种方法关闭更新 1.安装并登陆 2.打开 C:\ProgramData\NVIDIA Corporation 3.进入D ...

  3. 用EXCLE群发outlook邮件

    Outlookでメール一括送信する方法(差し込み.HTML形式.添付ファイル複数あり) メールを一括送信する方法はウェブ上にいくつも紹介されていましたが.以下のすべての条件を満たすものが見つからなかっ ...

  4. cometd简单用例

    准备工作 整个例子的源码下载:http://pan.baidu.com/s/1gfFYSbp 下载服务端jar文件 Comet4J目前仅支持Tomcat6.7版本,根据您所使用的Tomcat版本下载[ ...

  5. A. A Prank

    题意 有数列从小到大排列,都是不同范围1~ 1000,问你最多去掉多少个数字还能复原 由于wrong很多发所以写一下 链接 [http://codeforces.com/contest/1062/pr ...

  6. M2阶段事后总结报告

    会议照片: 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 开发一个快捷方便的记事本App.从用户体验角度出发,在一般记事本App的基础上进行创新 ...

  7. WEEK 7:团队项目的感想

    经过了几个星期的团队协作,我们的“爬虫”有了很大的完善,我作为团队中的主DEV,在这个过程中一边工作一边阅读,也有了不少的收获. Brooks的<没有银弹>告诉我们,在软件领域,没有什么绝 ...

  8. 小学四则运算APP 第二次冲刺-第二天

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第二次冲刺阶段时间:11.29~12.09 本次发布的判断题功能界面的设置: activity_panduan_set.xml: < ...

  9. git 使用ssh密钥

    一.查看仓库支持的传输协议 1.1查看仓库支持的传输协议 使用命令 git remote -v 查看你当前的 remote url root@zengyue:/home/yuanGit# git re ...

  10. octave基本指令1

    octave基本指令1 注释 使用: disp 输出指令 eg: >>a = pi; >>disp(sprintf('2 decimals:%0.2f'a)) 2 decima ...