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. ...
随机推荐
- MySQL的分区、分表、集群
1.分区 mysql数据库中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面(可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件,一个是frm存放表结构的,一 ...
- 快学Scala 第6章 对象 - 练习
1. 编写一个Conversions对象,加入inchesToCentimeters.gallonsToLiters和milesToKilometers方法. object Conversions { ...
- cocos creator 背景音乐音量和音效音量百分比设置
把音效的音量大小百分比保存在本地,播放音效的时候,带上音量大小,就像这样 你播放背景音乐时候,保存背景音乐的id,通过改变音量大小来调节背景音乐,就像这样 cc.audioEngine.setVolu ...
- 关于npm Vue
参考:http://www.runoob.com/w3cnote/vue2-start-coding.html 安装vue脚手架 npm install vue-cli -g 查看当前脚手架版本 np ...
- centos上部署应用到tomcat
tomcat部署JavaWeb项目分为下面的步骤: 1.删除tomcat按照目录的ROOT文件夹下的所有文件 2.把war包复制到ROOT文件下面,解压缩:jar -xvf xxxx.war; 3.结 ...
- (转载)关于管理计算机\\xp1 找不到网络路径的解决方案
关于管理计算机\\xp1 找不到网络路径的解决方案 使用域管理员登录域控DC,然后打开AD用户和计算机 选择一台域成员计算机,然后选择管理,结果出现如下提示:点击确定后出现如下提示随后,立刻用域管理员 ...
- Painter's Problem (高斯消元)
There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...
- html5+PHP,websocket无法连接的问题(Call to undefined function socket_create())
首先是配置文件的问题,打开extension=php_gd2.dll和extension=php_sockets.dll 扩展. 主要注意的是你当前系统使用的php版本和环境变量里面的php版本是否一 ...
- button theme
children:[ButtonTheme.bar( child:ButtonBar( children:[ FlatButton... ], ),), ]
- python爬虫——与不断变化的页面死磕和更新换代(3)
经过上一次的实战,手感有了,普罗西(雾)池也有了,再战taobao/tmall 试着使用phantomJS爬手机端,结果发现爬来的tmall页面全是乱码,taobao页面xpath识别错误.一顿分析了 ...