Rational Sum (20)

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)

题目描述

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

输入描述:

Each input file contains one test case. Each case starts with a positive integer N (<=100), 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.

输出描述:

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.

输入例子:

5
2/5 4/15 1/30 -2/60 8/3

输出例子:

3 1/3
#include<iostream>
#include<cstring>
#include<string>
using namespace std; long long gcd(long long x,long long y){
return y?gcd(y,x%y):x;
} long long lcm(long long x,long long y){
return x/gcd(x,y)*y;
} char s[]; int main(){
long long a=,b=; //a / b
int n;
cin >> n;
while(n--){
cin >> s;
char *t = strstr(s,"/");
if(t) *t = ' '; // a/b + c/d
long long c,d;
sscanf(s,"%lld %lld",&c,&d);
long long aa = a*d + b*c;
long long bb = b*d;
long long g = gcd((aa<)?(-aa):aa,bb);
a = aa/g;
b = bb/g;
}
long long x = a/b,y = a%b;
if(y == ) printf("%lld\n",x);
else{
if(x) printf("%lld ",x);
printf("%lld/%lld\n",y,b);
}
return ;
}

PAT Rational Sum的更多相关文章

  1. PAT 1081 Rational Sum

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

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

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

  3. PAT_A1081#Rational Sum

    Source: PAT A1081 Rational Sum (20 分) Description: Given N rational numbers in the form numerator/de ...

  4. PAT1081:Rational Sum

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

  5. pat1081. Rational Sum (20)

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

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

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

  7. PAT甲级——A1081 Rational Sum

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

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

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

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

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

随机推荐

  1. Java基础【基本数据类型包装类、int与String 之间的相互转换】

    为什么会有基本类型包装类? 将基本类型数据类型封装成对象,这样的好处可以在对象中定义更多方法操作该数据. 包装类常用的操作就是用于基本数据类型与字符串之间的转换 问题:int a=100; 为什么不能 ...

  2. Luncene学习二《搜索索引》

    搜索索引的流程 第一步:创建一个Directory对象,也就是索引库存放的位置 第二步:创建一个IndexReader对象,需要指定Directory对象 第三步:创建一个indexsearcher对 ...

  3. 说明Heap与stack的差别。

    Heap 是堆,Stack 是栈. 栈与堆都是Java用来在Ram中存放数据的地方,与C++不同,Java会自动管理栈与堆,程序员不能直接设置栈与堆. Java的堆是一个运行时的数据区,类的对象从中分 ...

  4. 4、iptables扩展匹配及网络防火墙功能

    关于centos7   firewalld    http://www.ibm.com/developerworks/cn/linux/1507_caojh/index.html 如何保存及重载规则: ...

  5. hihoCoder 1116 计算(线段树)

    http://hihocoder.com/problemset/problem/1116 题意: 思路: 用线段树解决,每个节点需要设置4个变量,sum记录答案,all记录整个区间的乘积,pre记录该 ...

  6. HDU 1403 Longest Common Substring(最长公共子串)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所 ...

  7. Centos7:查看某个端口被哪个进程占用

    查看端口被哪个进程使用的命令 netstat -lnp | grep 参考: https://blog.csdn.net/u010886217/article/details/83626236 htt ...

  8. python 拷贝文件

    使用绝对目录: import os import shutil shutil.copyfile("/opt/test/update.tar.gz","/opt/updat ...

  9. hibernate事务规范写法

    @Test public void testTx() { SessionFactory sessionFactory = null; Session session = null; Transacti ...

  10. HDUOJ 不容易系列之(4)——考新郎

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2049 一开始我的想法就是使用错排公式,先使用全排列从N对中选出M对,然后再使用错排对选出的M对进行错排计 ...