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 ...
随机推荐
- Android窗口系统第三篇---WindowManagerService中窗口的组织方式
Android窗口系统第一篇—Window的类型与Z-Order确定 Android窗口系统第二篇—Window的添加过程 上面文章梳理了一个窗口的添加过程,系统中有很多应用,每个应用有多个Activ ...
- linux命令学习笔记(62)-curl命令-url下载工具
linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合 传输工具,但按传统,习惯称url为下载工具. 一,curl命令参数,有好多我没有用过,也不知道 ...
- 物化视图基础概念、mview跨库迁移表
概念:物化视图是一种特殊的物理表,“物化”(Materialized)视图是相对普通视图而言的.普通视图是虚拟表,应用的局限性大,任何对视图的查询,Oracle都实际上转换为视图SQL语句的查询.这样 ...
- sublime插件insertDate显示ISO时间
1 下载insertDate插件以及安装完毕 2 把光标放在想插入ISO时间的地方 3 按住:alt+f5,之后,在sublime下面的Date format string输入:iso.之后按ente ...
- Mysql常用命令行大全(二)
#登录数据库mysql -hlocalhost -uroot -p;#修改密码mysqladmin -uroot -pold password new; #显示数据库show databases;#显 ...
- opencv报错 error: (-215) size.width>0 && size.height>0 in function cv::imshow
使用opencv读取摄像头并且显示事出现此问题: 后来发现是图像为空时的错误,加入: if(!frame.empty()) imshow("video",frame); 完整的代码 ...
- JavaScript高级程序设计学习笔记第十三章--事件
事件冒泡: IE 的事件流,事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播到较为不具体的节点(文档).例如: <!DOCTYPE html> <htm ...
- [PE182]RSA encryption
https://projecteuler.net/problem=182 题意: 找出满足下列条件的所有$e$ 的和, - $1 < e < \varphi \left( {1009,36 ...
- Ubuntu下如何禁用IPv6
Ubuntu下如何禁用IPv6 2013-10-16 11:32:02 分类: HADOOP 分布式下的hadoop/hbase运行总出问题,zookeeper连接总是出问题,怀疑可能是ip ...
- Laravel中使用Session存取验证码信息
1.将验证码存储到session中. $request->session()->put('validate_code',$validateCode->getCode());//存储信 ...