Source:

PAT A1081 Rational Sum (20 分)

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 where integeris 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的更多相关文章

  1. PAT1081:Rational Sum

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

  2. PAT 1081 Rational Sum

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

  3. PAT Rational Sum

    Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...

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

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

  5. pat1081. Rational Sum (20)

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

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

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

  7. A1081. Rational Sum

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

  8. 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 ...

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

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

随机推荐

  1. USB编程概念

     RE:USB编程概念 >请你指导我如何学习USB虚拟COM端口PROGRAMM,什么是我们needto有(如硬件和软件)的东西. USB是一个典型的复杂的协议栈,在协议的协议.这是很难完全实现 ...

  2. 二级域名解析设置及Apache 子域名配置

    域名管理解析项 如: cy.wanggangg.top 为wanggangg.top域名添加解析 主机记录设为 cy 记录值 为服务器ip地址 打开apache配置文件 新增如下:<Virtua ...

  3. web跨域

    之前对于跨域相关的知识一致都很零碎,正好现在的代码中用到了跨域相关的,现在来对这些知识做一个汇总整理,方便自己查看,说不定也可能对你有所帮助. 本篇主要内容如下: 浏览器同源策略 http 请求跨域 ...

  4. jsp页面间的传值方法

    JSP页面间传递参数是经常需要使用到的功能,有时还需要多个JSP页面间传递参数.下面介绍一下实现的方法. (1)直接在URL请求后添加 如:< a href="thexuan.jsp? ...

  5. Python django tests

    单元测试函数必须以test_开头,否则无法被识别

  6. Java开发最常犯的10个错误,打死都不要犯!

    原文:http://www.programcreek.com/2014/05/top-10-mistakes-java-developers-make/ 译文:cnblogs.com/chenpi/p ...

  7. ivew Upload 上传图片组件

    1. 先展示一个效果图 2.代码详解 <!-- 封面缩略图 --> <div class="pop-up-div pic"> <div class=& ...

  8. dentityServer4(1)- 特性一览

    本地应用只本地客户端应用,例如QQ.微信 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架.它可以在您的应用程序中提供以下功能: 它使 ...

  9. linux每日命令(4):解压命令

    1) Ubuntu 16.04 已经自动安装了unzip 软件,解压命令: unzip FileName.zip 2) 如果没有安装unzip,可以使用下面的命令安装: sudo apt instal ...

  10. maven环境变量配置及测试

    首先要确认,你是否已经安装了jdk,并且已经成功配置了环境变量. 和java配置类似,计算机右键属性->高级系统设置->环境变量. 添加如下变量: 变量名:maven_home 变量值:C ...