1081 Rational Sum(20 分)
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 (≤), 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<algorithm>
using namespace std;
typedef long long ll;
struct Fraction{
ll up,dowm;
}; ll gcd(ll a,ll b){
return b == ? a : gcd(b,a%b);
} Fraction reduction(Fraction result){
if(result.dowm < ){
result.up = - result.up;
result.dowm = - result.dowm;
}
if(result.up == ){
result.dowm = ;
}else{
int d = gcd(abs(result.dowm),result.up);
result.dowm /= d;
result.up /= d;
}
return result;
} Fraction add(Fraction f1,Fraction f2){
Fraction result;
result.up = f1.dowm*f2.up + f2.dowm*f1.up;
result.dowm = f1.dowm*f2.dowm;
return reduction(result);
} void showResult(Fraction r){
reduction(r);
if(r.dowm == ) printf("%lld",r.up);
else if(abs(r.up) > abs(r.dowm)){
printf("%lld %lld/%lld",r.up/r.dowm,r.up%r.dowm,r.dowm);
}else{
printf("%lld/%lld",r.up,r.dowm);
}
} int main(){
int n;
scanf("%d",&n);
Fraction sum,temp;
sum.up = , sum.dowm = ;
for(int i = ; i < n; i++){
scanf("%lld/%lld",&temp.up,&temp.dowm);
sum = add(sum,temp);
}
showResult(sum);
return ;
}
1081 Rational Sum(20 分)的更多相关文章
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- 1081. Rational Sum (20)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- pat1081. Rational Sum (20)
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
随机推荐
- 用js实现图片(小球)在屏幕上上下左右移动
<html> <head> <title>Document</title> </head> <body style="bac ...
- hdu5776sum
题目连接 抽屉原理:如果现在有3个苹果,放进2个抽屉,那么至少有一个抽屉里面会有两个苹果 抽屉原理的运用 现在假设有一个正整数序列a1,a2,a3,a4.....an,试证明我们一定能够找到一段连续 ...
- hdu-2874 Connections between cities(lca+tarjan+并查集)
题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/327 ...
- CentOS 6以下版本 支持Ext4
CentOS默认是不支持Ext4.所以你需要处理一下才行. 使用环境使用的是CentOS5.8 内核是 2.6.18-238.19.1.el5 其实CentOS 5.8 里面是有 ext4 模块的, ...
- P1955 [NOI2015]程序自动分析[离散化+并查集]
大水题一道,不明白为什么你谷评了个蓝.一看就是离散化,先去满足相等的条件,相等即为两点联通,或者说在同一个集合内.再看不相等,只有两元素在同一集合才不满足.裸的disjoint-set直接上,常数巨大 ...
- break、continue和return的区别
break.continue和return的区别 break function myBreak() { for(var i = 0; i < 5; i++) { if(i == 3) { bre ...
- 2010浙大:zoj问题
题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下:1. zoj能AC:2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者 ...
- Grunt:常见错误
ylbtech-Grunt:常见错误 1.返回顶部 1. D:\lab6\DS.JZB.Web>grunt build Running "clean:dist" (clean ...
- Jmeter查看结果树Unicode编码转中文方法
本文为转载微信公众号文章,如作者发现后不愿意,请联系我进行删除 在jmeter工具的使用中,不管是测试接口还是调试性能时,查看结果树必不可少,然而在查看响应数据时,其中的中文经常以Unicode的编码 ...
- 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
No valid host was found. There are not enough hosts available 'ascii' codec can't decode byte 0xe6 i ...