PAT_A1081#Rational Sum
Source:
Description:
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
whereinteger
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
Keys:
Code:
/*
Data: 2019-07-05 19:37:00
Problem: PAT_A1081#Rational Sum
AC: 26:24 题目大意:
给N个分数,求和
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M = 1e3;
struct fr
{
long long up;
long long down;
}temp; int gcd(int a, int b)
{
if(b==) return a;
else return gcd(b,a%b);
} fr Reduction(fr s)
{
if(s.up == )
s.down = ;
else
{
int d = gcd(abs(s.up), s.down);
s.up /= d;
s.down /= d;
}
return s;
} fr Add(fr s1, fr s2)
{
fr s;
s.up = s1.up*s2.down+s2.up*s1.down;
s.down = s1.down*s2.down;
return Reduction(s);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d\n", &n);
fr ans = fr{,};
for(int i=; i<n; i++)
{
scanf("%lld/%lld", &temp.up, &temp.down);
ans = Add(ans, temp);
} if(ans.down==)
printf("%lld\n", ans.up);
else if(ans.up >= ans.down)
printf("%lld %lld/%lld\n", ans.up/ans.down, abs(ans.up)%ans.down, ans.down);
else
printf("%lld/%lld\n", ans.up, ans.down); return ;
}
PAT_A1081#Rational Sum的更多相关文章
- PAT1081:Rational Sum
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- PAT Rational Sum
Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- pat1081. Rational Sum (20)
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- A1081. Rational Sum
Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...
- Twitter OA prepare: Rational Sum
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
随机推荐
- (转)GitHub上想下载单个文件方法
找到该文件,单机raw,如下图: 然后会在网页打开该文件,复制URL,下载即可(如果是不可预览文件,会自动下载). 转自: GitHub上想下载单个文件方法 - Smallcaff的博客 - CSDN ...
- 用Processing生成屏保(二)
代码 1: class TPoint 2: { 3: public TPoint(int _x, int _y) { 4: super(); 5: this._x = _x; 6: this._y = ...
- Python编写购物小程序
购物车要求: 用户名和密码存放于文件中 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够 ...
- Cocos2d-x在Windows平台环境的搭建
| 版权声明:本文为博主原创文章,未经博主允许不得转载. windows平台下配置Cocos2d-X引擎所需的压缩文件和可执行文件,如下: 下载链接: adt: http://tools.a ...
- Neo4j parameter
Neo4j browser: $ :help param Set a parameter Set a parameter to be sent with queries. The :param nam ...
- Java的部分问题和小结
2015/9/6 ThreadLocal:该类提供了线程局部变量,这样可以生成对每个线程唯一的局部标识符. 2015/9/18 1.乱码问题: js:xdata = encodeURI(encode ...
- tex, virtex, initex - 文本格式化和排版
SYNOPSIS 总览 tex [options] [commands] DESCRIPTION 描述 这份手册页并不全面.此版本的 TeX 完整的文档可以从 info 文件或者手册 Web2C: A ...
- Sql批量修改语句
修改某个数字类型字段 SET @num = 10000000000001; #定义初始化变量参数 UPDATE ckys_me #更新的表 SET openid = (@num := @num+1) ...
- Linux 100个常用指令
1.ls 列出目录内容. 文件属性: -:普通文件 d:目录文件 b:块设备 c:字符设备文件 l:符号连接文件 p:命令管道 s:套接字文件 文件权限: 9位数字,每3位一组 文件硬链接次数 文件所 ...
- MySQL练习题--sqlzoo刷题2
SELECT from Nobel Tutorial 1.Change the query shown so that it displays Nobel prizes for 1950. SELEC ...