PAT 1081 Rational Sum
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<bits/stdc++.h>
using namespace std;
typedef long long ll; // a/a1 + b/b1 ll to_int(string s){
ll sum = ; bool flag = ;
if(s[] == '-'){
flag = ;
s = s.substr(,s.size()-);
}
for(int i=;i < s.size();i++){
sum = sum* + (s[i]-'');
}
if(flag) return sum;
else return -sum;
} ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
} // a/a1 + b/b1 int main(){
int t;cin >> t;
ll a = ,a1 = ; while(t--){
string s; cin >> s;
int pos = s.find('/');
string stra = s.substr(,pos);
string stra1 = s.substr(pos+,s.size()-pos);
ll b = to_int(stra);
ll b1 = to_int(stra1); ll c = a*b1 + b*a1;
ll c1 = a1*b1; ll t = gcd(c,c1); a = c/t;
a1 = c1/t;
} ll x = a/a1;
ll y = a%a1; if(x&&y){printf("%lld %lld/%lld",x,y,a1);}
else if(!x&&y){printf("%lld/%lld",y,a1);}
else if(x&&!y)printf("%lld",x);
else printf(""); return ;
}
——一开始把输出写错了。。
PAT 1081 Rational Sum的更多相关文章
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 1081. Rational Sum (20)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- 1081 Rational Sum(20 分)
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
随机推荐
- Spring 学习教程(一):浅谈对Spring IOC以及DI的理解
一.个人对IoC(控制反转)和DI(依赖注入)的理解我们平时在开发java web程序的时候,每个对象在需要使用它的合作对象时,自己都要将它要合作对象创建出来(比如 new 对象),这个合作对象是由自 ...
- 使用dbeaver查mysql的表会导致锁表的问题
查询完成之后接着需要使用rollback,不然其它session没法执行语句.
- linux之tree命令
linux下tree命令的用法解释 2018年03月22日 ⁄ RakSmart教程 ⁄ 共 583字 ⁄ 字号 小 中 大 ⁄ linux下tree命令的用法解释已关闭评论 tree命令,主要功能是 ...
- 5.Python3程序结构
5.1顺序结构 一条语句一条语句顺序的执行. 5.2选择结构 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: 5.2. ...
- python头部 #!/usr/bin/env python
*.py运行: python *.py OR ./*.py 对于*.py其首行应标明 #!/usr/bin/env python,定义python解释器调用路径,对比#!/usr/bin/python ...
- html5 javascript 新增加的高级选择器更精准更实用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- html5与css3面试题(1)
1.html有哪些新特性移除了哪些元素 新特性: 语义化标签(header,nav,footer.section) 音频,视频(audio,video) 画布canvas 移除元素: 表现元素:cen ...
- GoldenGate for Java adapter介绍二(代码篇)
本示例主要介绍通过实现OGG的接口函数,实现自定义处理增量数据,将数据实时写入到mariadb (OGG官方不支持此数据库,所以只能采用自定义方式实现).以下是本次示例的4个类: Connection ...
- django+nginx+python3 生产环境部署
一.安装python基础环境 1.安装各类基础模块 yum install gcc-c++ wget openssl-devel bzip2-devel expat-devel gdbm-devel ...
- MySQL插入更新_ON DUPLICATE KEY UPDATE
前提:操作的表具有主键或唯一索引 INSERT INTO:表中不存在对应的记录,则插入:若存在对应的记录,则报错: INSERT INTO IGNORE:表中不存在对应的记录,则插入:若存在对应的记录 ...