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 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
 #include <iostream>
#include <vector>
#include <math.h>
using namespace std;
long int gcd(long int a, long int b)
{
if(b==) return a;
else return gcd(b,a%b);
}
int main()
{
int N;
cin >> N;
long long Inter = , resa = , resb = , a, b, Div, Mul;
for (int i = ; i < N; ++i)
{
char c;
cin >> a >> c >> b;
resa = resa * b + a * resb;//同分相加的分子
resb = resb * b;
Inter += resa / resb;//简化
resa = resa - resb * (resa / resb);
Div = gcd(resb, resa);
resa /= Div;
resb /= Div;
}
if (Inter == && resa == )
cout << << endl;
else if (Inter != && resa == )
cout << Inter << endl;
else if (Inter == && resa != )
cout << resa << "/" << resb << endl;
else
cout << Inter << " " << resa << "/" << resb << endl;
return ;
}

PAT甲级——A1081 Rational Sum的更多相关文章

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

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

  2. A1081. Rational Sum

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

  3. PAT甲级——A1088 Rational Arithmetic

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

  4. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

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

  5. 【PAT甲级】1104 Sum of Number Segments (20 分)

    题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...

  6. PAT_A1081#Rational Sum

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

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  8. PAT 1081 Rational Sum

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

  9. PAT Rational Sum

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

随机推荐

  1. centOs 查看系统cpu使用率等--top

    原文:centOs 查看系统cpu使用率等--top 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CP ...

  2. input输入内容成可点击状态

    <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1. ...

  3. Ubantu18.04安装WPS

    1.去WPS官网选在合适的版本下载安装包2.在官网下载字体包3.分别右键点击安装包,选择第一项“用软件安装打开”,进行安装即可.4.此时启动应用,应该会提示系统缺失字体.5.解决字体缺失(转)    

  4. 时间 '2018-08-06T10:00:00.000Z' 格式转化为本地时间(转)

    原文:https://blog.csdn.net/sxf_123456/article/details/81582964 from datetime import datetime,timedelta ...

  5. excel 导数据

    参考: ="insert tsilverinfo(ss_id,memo,ss_weight,ts_id,ss_type,ModelPosX,ss_stoneW,ss_stoneWU) val ...

  6. linux 下使用scp命令传输文件

    scp -P 1234 /home/wakasann/test.txt wakasann@192.168.1.30:/var/www/html/ 使用 1234端口,将 test.txt文件传输到服务 ...

  7. A decorative fence

    A decorative fence 在\(1\sim n\)的全排列\(\{a_i\}\)中,只有大小交错的(即任意一个位置i满足\(a_{i-1}<a_i>a_{i+1}ora_{i- ...

  8. (转)第04节:Fabric.js用路径画不规则图形

    在Canvas上画方形.圆形.三角形都是很容易的,只要调用fabric对应的方法就可以了,但这些都是规则的图形,如果你想画一个不规则的图形,这时候你可以用fabric.js提供的路径绘图方法.所谓路径 ...

  9. django中写form表单时csrf_token的作用

    之前在学习django的时候,在template中写form时,出现错误.百度,google后要加{% csrf_token %}才可以,之前一直也没研究,只是知道要加个这个东西,具体是什么也不明白. ...

  10. windows sdk版本 之 并查集生成迷宫

    #include <cstdlib> #include <ctime> #include<algorithm> using namespace std; exter ...