题目描述

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 <bits/stdc++.h>
using namespace std;
int main(){int n;scanf("%d",&n);long long sum1,sum2,a,b;for(int i=0;i<n;i++){scanf("%lld/%lld",&a,&b);if(i==0) sum1=a,sum2=b;else sum1=sum1*b+a*sum2,sum2*=b;int k=__gcd(sum1,sum2);sum1/=k;sum2/=k;}long long p,q,r;p=sum1/sum2;q=sum1%sum2;if(sum1*sum2<0) q*=-1;r=abs(sum2);if(p!=0)printf("%lld ",p);if(q!=0)printf("%lld/%lld",q,r);if(p==0&&q==0)printf("0");return 0;
}

【PAT】Rational Sum相关推荐

  1. 【PAT】乙级题目解答合集(c++)

    [PAT]乙级题目解答合集(c++) 本篇文章为对PAT乙级1001-1095的题目解答的汇总 1001 害死人不偿命的(3n+1)猜想 (15 分) 1002 写出这个数 (20 分) 1003 我 ...

  2. PAT A1081 Rational Sum ——垂死病中惊坐起

    PAT A1081 Rational Sum 想不起来最小公倍数咋求了,可是我为啥要用最小公倍数通分呢... 最后一个测试点是结果=0的情况 最好全程使用long long,尤其注意输入的时候,如果用 ...

  3. 【PAT】第四章 算法初步

    第四章 算法初步 目录 第四章 算法初步 4.1 排序 4.1.1 选择排序 4.1.2 插入排序 4.1.3 排序题与sort函数的应用 strcmp 计算排名 4.2 散列 4.2.1 散列 1. ...

  4. 【HDOJ】 1001 Sum Problem_天涯浪子_新浪博客

    [题目] http://acm.hdu.edu.cn/showproblem.php?pid=1001 [报告] 简单的求和,不过得考虑中间溢出的情况,不能直接用n*(n+1)/2求和. [程序] / ...

  5. 【PAT】PAT总结《搜索、打表、分治、排序算法、队列、栈、堆、Hash》

    打表 打表这个技巧其实我们在素数部分的题已经涉及到了.还是要仔细思考,什么时候应该打表,打表有助于减少后续的计算的时候,我们应该打表, 特别是查询量特别大,即时计算无论多么快都会超时的情况.或者逆向运 ...

  6. 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)

    在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...

  7. 【PAT】(B)1094 谷歌的招聘 (20 分)

    『题意描述』 2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘.内容超级简单,就是一个以 .com 结尾的网址,而前面的网址是一个 10 位素数,这个素数是 ...

  8. 【PAT】A1079 Total Sales of Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. 【PAT】A1090 Highest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

最新文章

  1. 7、Power Map—实例:添加二维数据表以及批注
  2. boost::type_erasure::callable相关的测试程序
  3. 银行应用_讲解人脸识别在银行的应用
  4. macos安装低版本php,mac下安装多个版本PHP及切换
  5. php mysql 写法_php 类的写法
  6. python通讯录综合_Python通讯录作业
  7. 33款你可能不知道的开源爬虫软件工具
  8. NSDictionary / NSMutableDictionary 及 NSArray / NSmutableArray (实例)
  9. Vista暴力破解器只是一个玩笑 谁说破解谁撒谎
  10. 兄弟连Linux教学——第三讲 给初学者的建议
  11. Deepin中如何使用adb和fastboot工具
  12. Firefox在win10与win11系统上账号不同步的解决办法
  13. 【网络安全】加解密算法最详解
  14. java识别验证码图片_Java识别图像、验证码
  15. Macropodus 新词发现详解(new word discovery, python3)
  16. HardFault错误信息分析定位
  17. 配合屏幕录像专家,又小又清晰!
  18. 工作 7 年的老程序员,现在怎么样了
  19. 关于LSB图片隐写的解法
  20. java代理(静态代理和jdk动态代理以及cglib代理)

热门文章

  1. SSD性能优化之4K对齐
  2. JavaScript使用setInterval()实现真实进度条
  3. JS简单实现进度条效果
  4. 大数据分析案例-基于随机森林算法探究电商网站推销商品的影响因素
  5. 电脑服务器显示一个f盘损坏,F盘异常如何修复呢?
  6. 阿里组织调整:张勇兼任阿里云总裁,达摩院肩负更大使命
  7. 全球与中国半导体晶圆金属剥离平台市场深度研究分析报告
  8. 智能电视+android+4.0接收视频,智能电视Android4.0系统魅力小析
  9. matlab中的隐马尔可夫模型(HMM)实现
  10. 有理函数积分的一般解法