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. 使用 Linux 的 strace 命令跟踪/调试程序的常用选项

    原文:http://linoxide.com/linux-command/linux-strace-command-examples/作者: Raghu 在调试的时候,strace能帮助你追踪到一个程 ...

  2. ssh命令大全

    常用格式:ssh [-l login_name] [-p port] [user@]hostname 更详细的可以用ssh -h查看. 举例 不指定用户: ssh 192.168.0.11 指定用户: ...

  3. 如何在SpriteBuilder中使用BM Font Label

    开始不知道,还真有点小繁琐. mac系统上创建BM Font的工具有不少,我主要用hiero和GlyphDesigner:前者是java写的,后者是mac原生的,功能都差不多. 还有一个类似的工具bm ...

  4. 网站开发进阶(十五)JS基础知识充电站

    JS基础知识充电站 1.javascript alert弹出对话框时确定和取消两个按钮返回值? 用的不是alert对话框,是confirm confirm(str); 参数str:你要说的话或问题: ...

  5. 分别修改Cube每个面的贴图UV(Unity3D开发之十八)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46611169 ...

  6. PS 图像特效算法— —渐变

    这个特效利用图层的混合原理,先设置一个遮罩层,然后用遮罩层与原图进行相乘,遮罩层不同,图像最后呈现的渐变效果也不一样. clc;clear all;close all;addpath('E:\Phot ...

  7. Android高效率编码-细节,控件,架包,功能,工具,开源汇总,你想要的这里都有

    Android高效率编码-细节,控件,架包,功能,工具,开源汇总 其实写博客的初衷也并不是说什么分享技术,毕竟咱还只是个小程序员,最大的目的就是对自我的知识积累,以后万一编码的时候断片了,也可以翻出来 ...

  8. 数据结构之---二叉树C实现

    学过数据结构的都知道树,那么什么是树? 树(tree)是包含n(n>0)个结点的有穷集,其中: (1)每个元素称为结点(node): (2)有一个特定的结点被称为根结点或树根(root). (3 ...

  9. HBase 协处理器实现二级索引

    HBase在0.92之后引入了coprocessors,提供了一系列的钩子,让我们能够轻易实现访问控制和二级索引的特性.下面简单介绍下两种coprocessors,第一种是Observers,它实际类 ...

  10. 在java中读取配置文件信息

    public class PropertyUtil { public static final Properties PROP = new Properties(); /** * 读取配置文件的内容( ...