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. ...
随机推荐
- vue 中使用 axios 请求接口,请求会发送两次问题
在开发项目过程中,发现在使用axios调用接口都会有两个请求,第一个请求时,看不到请求参数,也看不到请求的结果:只有第二次请求时才会有相应的请求参数以及请求结果: 那为甚么会有这么一次额外的请求呢,后 ...
- Jmeter学习之-从数据库取出数据并且校验数据是否准确
https://www.cnblogs.com/wuyonghuan/p/7479582.html 应用场景:调用某个接口像数据库中插入数据,需要在接口调用完成后查看数据更新或插入的数据是否正确的时候 ...
- IDEA--生成jar包并且导出jar包
PS:首先在idea中新建一个java文件,且带有main方法(不带有main好像不能导出,不确定) 参考文章:http://www.cnblogs.com/blog5277/p/5920560.ht ...
- openshift 容器云从入门到崩溃之十《容器监控-数据展示》
POD资源历史曲线(CPU.内存.网络) 监控方案heapster+hawkular-metrics+hawkular-cassandra heapster负责收集数据 hawkular-cassan ...
- 如何发布第一个属于自己的npm包 到公网
发布前的准备 1. 注册一个npm账号 前往NPM官网进行注册 2. 创建一个简单的包 在本地创建一个项目文件夹 mannymu_demo (名字自己取,不要和NPM上已有的包名重复冲突就好)然后通过 ...
- postman 做接口测试之学习笔记
Postman 之前是作为Chrome 的一个插件,现在要下载应用才能使用. 以下是postman 的界面: 各个功能区的使用如下: 快捷区: 快捷区提供常用的操作入口,包括运行收藏夹的一组测试数据, ...
- C# AsyncCallback异步回调用法示例
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- xshell 利用密钥登录
第一步:新建用户密钥 第二步:选择加密方式,密钥长度越长越安全 第三步:设置密钥名称和密码(密码可为0,这里是密钥的密码非服务器密码) 第四步:保存公钥到本地 第五步:导出私钥到本地 第六步:将公钥和 ...
- videojs+hls+rtmp流媒体播放
前言:刚接触前端,小白一个,如有出错,请指正,谢谢! 注:视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类. 一.互联网网络流媒体简介 HTTP stream是各 ...
- Python 用户交互程序Input
1.用户交互,用户输入 用户输出命令是input. 设置一个变量并输出. name = input("name:") print(name) 注:python2 下 raw_inp ...