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 ...
随机推荐
- npm ERR! cb() never called! npm ERR! This is an error with npm itself. Pleas
原因是:需要用管理员的身份才能进行 方法:点开始,找到命令提示符,右键,点以管理员的身份运行命令即可
- 遵循PEP8风格指南
整理来自 effective python 一 空白 1 使用space来标识缩进,而不是tab 2 和语法相关的每层缩进都使用4个空格 3 每行的字符数不应该超过79 4 对于多行的表达式,除了首行 ...
- activiti7从act_ge_bytearray表中查询资源文件并保存到桌面文件夹中
package com.zcc.activiti02; import org.activiti.engine.ProcessEngine;import org.activiti.engine.Proc ...
- Django框架(二十五)—— Django rest_framework-路由控制与响应器
路由控制与响应器 一.路由控制 # 1.基本路由: url(r'^publish/$', views.PublishView.as_view()), # 2.半自动路径:views.PublishVi ...
- Java并发编程教程
Java是一种多线程编程语言,我们可以使用Java来开发多线程程序. 多线程程序包含两个或多个可同时运行的部分,每个部分可以同时处理不同的任务,从而能更好地利用可用资源,特别是当您的计算机有多个CPU ...
- 在已有QT项目中添加多个UI布局界面
1.在工程中右键->添加新文件,按图选择 2.选择窗口部件 3.创建UI控制类 注意上图红框中命名按实际需要定义,否则后期改动要修改UI文件参数 4.修改UI文件,框1是窗口部件父类,框2是UI ...
- 37-python基础-python3-字典的常用方法-keys()-values()-items()
有 3 个字典方法,它们将返回类似列表的值,分别对应于字典的键.值和键-值对:keys().values()和 items(). 这些方法返回的值不是真正的列表,它们不能被修改,没有append()方 ...
- moment.js 快捷查询
格式化日期 当前时间: moment().format('YYYY-MM-DD HH:mm:ss'); //2014-09-24 23:36:09 今天是星期几: moment().format('d ...
- linux系统:go build报错import cycle not allowed
go build 困扰我多时的 go 编译报错:循环导入,代码肯定是没问题的,网上查说重新安装go 我觉得也不是太好的办法 import cycle not allowed package day01 ...
- List、Map、Set三个接口存取元素时,各有什么特点
List接口以特定索引来存取元素,可以有重复元素 Set接口不可以存放重复元素(使用equals方法区分是否重复) Map接口保存的是键值对(key-value-pair)映射,映射关系可以是一对一或 ...