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<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的更多相关文章

  1. PAT 1081 Rational Sum[分子求和][比较]

    1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...

  2. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...

  3. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

    题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...

  4. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  5. 【PAT甲级】1081 Rational Sum (20 分)

    题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...

  6. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  7. 1081. Rational Sum (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...

  8. 1081. Rational Sum (20) -最大公约数

    题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...

  9. 1081 Rational Sum(20 分)

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

随机推荐

  1. 学习笔记:Makefile的ifeq逻辑或,逻辑与的变通实现

        (1)ifeq的用法 ifeq ($(变量名), 变量值 ) ........ else ifeq ($(..), ..) ......... else ......... endif (2) ...

  2. 关于SQL视图的创建和使用方法

    SQL视图的创建和使用 视图这个东西在其他的软件中,我们也经常看得到,例如word中就有视图,我们不难发现,视图似乎是一种显示方式,就像WORD一样,有几种显示版面,这就是视图,那么SQL中的视图又该 ...

  3. sql 小技巧

    declare @pids varchar(max)='' ),pid)+','+@pids from product where pname like '%red%' select @pids

  4. Eclipse安装svn插件的几种方式(转)

    1.在线安装: (1).点击 Help --> Install New Software... (2).在弹出的窗口中点击add按钮,输入Name(任意)和Location(插件的URL),点击 ...

  5. oracle导出导入指定表

    从源数据库导出: exp user1/pwd@server1/orcl file=c:\temp\exp.dmp tables=(table1, table2) 导入到目标数据库: imp user2 ...

  6. element table 二次封装 父子组件传值 组件通信

    新建一个组件(即子组件)table.vue 子组件编辑内容如下图所示 子组件通过props获取父组件传递过来的参数,如下图所示,type指明传递到子组件的数据类型,default指定默认值,一般不给 ...

  7. Python 协程实现socket并发

    socket多并发 socket可以实现单个客户端进行请求访问,它可以通过socketserver来实现并发功能呢,socketserver是通过启用多线程实现并发,在这里我们也可以通过gevent协 ...

  8. python学习笔记:深浅拷贝的使用和原理

    在理解深浅拷贝之前,我们先熟悉下变量对象和数据类型 1.变量和对象 变量-引用-对象(可变对象,不可变对象) 在python中一切都是对象,比如[1,2],'hello world',123,{'k1 ...

  9. 单元测试系列之一:如何使用JUnit、JaCoCo和EclEmma提高单元测试覆盖率

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢!   原文链接:http://www.cnblogs.com/zishi/p/6726664.html -----如 ...

  10. 用bytomswap进行“跨链”资产转换

    bytom是专注资产领域的公有区块链平台,最近开发者社区基于比原做了一款资产转换平台.我们可以在上面通过自己现有的资产在比原上发行资产.然后达到资产转换的目的. 一. 以太币资产转换成比原上的资产 首 ...