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 (≤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.

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 <cstdio>
#include <algorithm>
using namespace std;typedef long long ll;
struct Fenshu{ll up;ll down;
};ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);
}int main(){int n;Fenshu temp,sum;sum.up=0;sum.down=1; scanf("%d",&n);for(int i=0;i<n;i++){scanf("%lld/%lld",&temp.up,&temp.down);sum.up=sum.up*temp.down+temp.up*sum.down;sum.down=sum.down*temp.down;ll d=gcd(abs(sum.up),abs(sum.down));sum.up=sum.up/d;sum.down=sum.down/d;}if(sum.down==1){printf("%lld\n",sum.up);}else if(abs(sum.up)>sum.down){printf("%lld %lld/%lld\n",sum.up/sum.down,abs(sum.up)%sum.down,sum.down);}else{printf("%lld/%lld\n",sum.up,sum.down);}return 0;}

1081 Rational Sum (20 分) 分数计算+最大公约数相关推荐

  1. 1081 Rational Sum (20 分) 分数加法 简单模拟

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

  2. 1081 Rational Sum (20 分)_22行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given N rational numbers in the form numerator/denominator, you a ...

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

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

  4. 1081. Rational Sum (20)-PAT甲级真题

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

  5. PAT甲级1081 Rational Sum:[C++题解]分数求和、辗转相除法求最大公约数、long long有一个数据溢出

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析 数据量很小,直接模拟分数加法即可,分数加法如下: ab+cd=ad+bcbd\frac{a}{b}+\frac{c}{d}=\frac ...

  6. PAT甲级 1081 Rational Sum 分数相加的和

    代码如下: //求分数相加的和 #include<iostream> #include<stdio.h> #include<math.h> using namesp ...

  7. 1081 Rational Sum 有理数类型题处理 需再做

    一.有理数结构体的几个约束 struct fraction{LL up,down;fraction(LL _up,LL _down):up(_up),down(_down){} }; (1)如果这个有 ...

  8. 1088 Rational Arithmetic (20 分)【难度: 简单 / 知识点: 模拟】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805378443755520 PAT乙级的原题,直接模拟即可 #inclu ...

  9. C++学习之路 | PTA乙级—— 1063 计算谱半径 (20 分)(精简)

    1063 计算谱半径 (20 分) 在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的 n 个复数空间的特征值 { a ​1 ​​ +b ​1 ​​ i,⋯ ...

  10. 7-2367 计算职工工资(20 分)

    7-2367 计算职工工资(20 分) 给定N个职员的信息,包括姓名.基本工资.浮动工资和支出,要求编写程序顺序输出每位职员的姓名和实发工资(实发工资=基本工资+浮动工资-支出). 输入格式: 输入在 ...

最新文章

  1. 翻译连载 | 第 9 章:递归(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇...
  2. 用Python解“支票问题”题
  3. python配置opencv最简单_【萌新】面向(Windows10)python的opencv环境配置“个人向”报错总结...
  4. 华为服务器忘记管理密码怎么修改,忘记云服务器密码怎么修改
  5. Bootstrap组件_按钮组
  6. java与c++中的对象序列化分析
  7. SAP Spartacus NgExpressEngineDecorator 的工作原理
  8. Unity插件Gaia使用介绍
  9. MySQL-ProxySQL中间件Admin Schemas介绍
  10. MYSQL详解及语法大全
  11. iphone5信号无服务器,南京苹果维修点告诉你iPhone手机显示无信号、wifi故障该怎么处理?...
  12. 【Circulation love恋爱循环】MMD镜头+动作打包下载.zip
  13. OpenCV——图像连通域
  14. NSString 和 UInt8 相互转换
  15. MODBUS RTU 协议读卡器
  16. Docker基础认识与docker安装以及环境配置
  17. 最近在做一些改变,想听听你的意见
  18. python怎么输出坐标_使用Python实现图像标记点的坐标输出功能
  19. IEC61850开发流程
  20. 1. 【Part3】 Contour Detection and Hierarchical Image Segmentation【轮廓检测图像分割】

热门文章

  1. 腾讯Android自动化测试实战3.2.1 Robotium支持Native原理
  2. 学习JAVA环境搭建之下载安装MyEclipse 6.5
  3. Java集合(十三)Iterator和Enumeration的区别和对比
  4. iOS设备是否越狱的判断代码
  5. windows下python访问ipv6报错
  6. 西门子的十一位CEO
  7. Java——包装器类
  8. linux 查看各个用户下线程使用情况
  9. redhat 复制文件夹及子文件夹_Docker COPY 复制文件夹的诡异行为
  10. html在ie中img地址为https,关于IE10以下的img标签问题解决