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. ...
随机推荐
- typescript 创建类型
type long = string | number; type stringObj = { [index: string]: string; } type NumberObj = { [index ...
- 16.0-uC/OS-III同步
同步 uC/OS-III中用于同步的两种机制:信号量和事件标志组 . 1.信号量 信号量最初用于控制共享资源的访问.信号量可用于ISR与任务间.任务与任务间的同步. “ N”表示信号量可以被累计.初始 ...
- 接口测试工具-Jmeter使用笔记(九:跨线程组传递变量)
使用场景: 请求API需要授权令牌,但是授权令牌只需要获取一次,即可调用服务器上其他业务接口. 所以我想要把授权操作放在单独的一个线程,业务流放在其他线程. 这就需要我把从授权线程获取的令牌传入业务流 ...
- java框架之SpringBoot(13)-检索及整合Elasticsearch
ElasticSearch介绍 简介 我们的应用经常需要使用检索功能,开源的 Elasticsearch 是目前全文搜索引擎的首选.它可以快速的存储.搜索和分析海量数据.SpringBoot 通过整合 ...
- [py]GTM和UTC及python的时间戳
时间戳是一串字符串 time.time() 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数.通俗的讲, 时间戳是一 ...
- Cocos Creator 触摸点击事件
触摸事件// 使用枚举类型来注册枚举对象定义 对应的事件名 事件触发的时机cc.Node.EventType.TOUCH_START 'touchstart' 当手指触点落在目标节点区域内时cc.No ...
- CSS实现经典的三栏布局
实现效果: 左右栏定宽,中间栏自适应 (有时候是固定高度) 1 . 绝对定位布局:position + margin <div class="container"> & ...
- python-15
递归特性: 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少 3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种 ...
- shell中输出日期的一个函数
开始 function T () { date '+%F %T.%N' } 结束
- 既然选择了远方,便只顾风雨兼程--myvue
浅谈以下vue的模式,其实vue的模式跟react是一样的,都是MVVM模式,就是直接数据和视图之间的切换 如果单纯这样认识的话,和angular相比较起来,vue就简单的很多,但是事实情况并不是这样 ...