1081. Rational Sum (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

思路

分子相加的运算。
1.辗转相除法求分子分母的最大公约数
2.两分数相加后要化简,不然容易在计算时产生溢出。
3.输出需要特别注意的格式:
1)在整数不为0的情况下,分数为0则只输出整数。
2)在整数为0的情况下,分数不为0则只输出分数。
3)二者都为0直接输出一个0。
4)二者都不为0按题目要求的标准格式输出。
4.关于分母为0的情况,题目测试用例好像并未考虑,暂不做处理。 代码
#include<iostream>
using namespace std;
typedef long long ll; ll gcd(ll a,ll b) //求最大公约数
{
return b == 0?abs(a):gcd(b,a % b);
}
int main()
{
ll N,a,b,gvalue,suma,sumb;
while( cin >> N)
{
suma = 0,sumb = 1;
for(int i = 0;i < N;i++)
{
scanf("%lld/%lld",&a,&b);
gvalue = gcd(a,b);
//约分
a /= gvalue;
b /= gvalue;
//分数求公倍数相加
suma = a * sumb + b * suma;
sumb = b * sumb;
//分子和约分
gvalue = gcd(suma,sumb);
suma /= gvalue;
sumb /= gvalue;
}
ll integer = suma / sumb;
ll numerator = suma - integer * sumb;
if(integer != 0)
{
cout << integer;
if(numerator != 0)
{
cout << " ";
printf("%lld/%lld",numerator,sumb);
}
}
else
{
if(numerator != 0)
{
printf("%lld/%lld",numerator,sumb);
}
else
cout << 0;
}
cout << endl;
}
}

  

PAT1081:Rational Sum的更多相关文章

  1. pat1081. Rational Sum (20)

    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. PAT_A1081#Rational Sum

    Source: PAT A1081 Rational Sum (20 分) Description: Given N rational numbers in the form numerator/de ...

  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. 一个简单的多机器人编队算法实现--PID

    用PID进行领航跟随法机器人编队控制 课题2:多机器人编队控制研究对象:两轮差动的移动机器人或车式移动机器人研究内容:平坦地形,编队的保持和避障,以及避障和队形切换算法等:起伏地形,还要考虑地形情况对 ...

  2. 《java入门第一季》之面向对象(static关键字内存图解)

  3. oracle临时表空间 ORA-01652:无法通过16(在表空间XXX中)扩展 temp 字段

    今天在查数据的时候报错  ORA-01652:无法通过16(在表空间temp1中)扩展 temp 字段 查看表空间使用明细 SELECT b.tablespace,        b.segfile# ...

  4. ITU-T E.800 有关服务质量(QoS)的术语定义

    摘要 ITU-T E.800建议书为服务质量(QoS)的研究和管理提供了一套通用术语.本建议书列出的与QoS相关的技术和非技术术语旨在代表电信市场所有各方(即用户.服务提供商.制造商和监管机构)的利益 ...

  5. adb shell后出现error解决方案

    解决办法: 解决办法: 1.adb kill-server 2.adb start-server 3.adb remount 4.adb shell 一般情况下都可以在此启动adb相关

  6. C++中的new/delete

    不同于C语言中的malloc/free是库函数,C++语言中的new/delete是运算符,而不是库函数. new/delete执行流程 我们经常会接触到的是new/delete operator(就 ...

  7. OpenCV——PS 滤镜, 曝光过度

    算法原理可以参考: PS 滤镜,曝光过度 #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include <io ...

  8. thrift实现HDFS文件操作

    thrift 文件如下 namespace java com.pera.file.transform struct  File{     1:string path ,     2:string co ...

  9. Android绘图机制(三)——自定义View的实现方式以及半弧圆新控件

    Android绘图机制(三)--自定义View的三种实现方式以及实战项目操作 在Android绘图机制(一)--自定义View的基础属性和方法 里说过,实现自定义View有三种方式,分别是 1.对现有 ...

  10. Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!

    Android高效率编码-第三方SDK详解系列(一)--百度地图,绘制,覆盖物,导航,定位,细腻分解! 这是一个系列,但是我也不确定具体会更新多少期,最近很忙,主要还是效率的问题,所以一些有效的东西还 ...