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. spring boot 集成Druid

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...

  2. MySQL针对Swap分区的运维注意点

    Linux有很多很好的内存.IO调度机制,但是并不会适用于所有场景.对于运维人员来说,Linux比较让人头疼的一个地方是:它不会因为MySQL很重要就避免将分配给MySQL的地址空间映射到swap上. ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 12.24daily_scrum

    今天是平安夜,大家开心地度过一个平安夜的同时,也完成了很多软件的调试工作,我们争取在下周前完成本阶段的所有调试工作. 具体工作如下: 具体工作: 小组成员 今日任务 明日任务 工作时间 李睿琦 软件调 ...

  5. 团队作业Week14——源代码管理

    0. 在吹牛之前,先回答这个问题: 如果你的团队来了一个新队员,有一台全新的机器, 你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新.最稳定版本的软件编译出 ...

  6. LINUX内核分析第五周学习总结——扒开系统调用的“三层皮”(下)

    LINUX内核分析第五周学习总结--扒开系统调用的"三层皮"(下) 标签(空格分隔): 20135321余佳源 余佳源 原创作品转载请注明出处 <Linux内核分析>M ...

  7. 解决AJAX session跨域失效

    1.想实现的功能是登录时有个验证码,这个验证码后台提供,然后放在session中,前台把用户输入的验证码通过AJAX发给后台,后台把session中的验证码取出来然后比较不同,一样则通过. 问题出现在 ...

  8. 请求数据传入(SpringMVC)

    1.    请求处理方法签名 Spring MVC 通过分析处理方法的签名,HTTP请求信息绑定到处理方法的相应人参中. Spring MVC 对控制器处理方法签名的限制是很宽松的,几乎可以按喜欢的任 ...

  9. PAT L2-011 玩转二叉树

    https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784 给定一棵二叉树的中序遍历和前序遍历,请你先将 ...

  10. #Leetcode# 788. Rotated Digits

    https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...