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. 21个CSS技巧

    级联样式表(CSS)在当代Web设计中已经成为重要的环节,如果没有CSS现在的网站将像10年前一样不堪入目.随着CSS技术的普及,越来越多的高质量CSS教程涌入互联网,让我们的学习更加方便. 1.CS ...

  2. 流计算与Hadoop

  3. 深度探索C++对象模型之第二章:构造函数语意学之Default constructor的构造操作

    C++新手一般由两个常见的误解: 如果任何class没有定义默认构造函数(default constructor),编译器就会合成一个来. 编译器合成的的default constructor会显示的 ...

  4. 2019.12.04 Java中的内存分配

    Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入Java核心,详细介绍一下Java在内存分配方面的知识.一般 ...

  5. php设置时区和strtotime转化为时间戳函数

    date_default_timezone_set('PRC');//设置中华人民共和国标准时间 strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 格式:int strto ...

  6. TopCoder代码格式模板

    $BEGINCUT$ $PROBLEMDESC$ $ENDCUT$ #include<bits/stdc++.h> using namespace std; class $CLASSNAM ...

  7. python相关软件安装流程图解——虚拟机操作——复制虚拟机主机——CentOS-7-x86_64-DVD-1810

    请先确保已经安装了虚拟机 python相关软件安装流程图解——虚拟机安装——CentOS-7-x86_64-DVD-1810——CentOS-01下载 https://www.cnblogs.com/ ...

  8. NOI2014

    听说14,15年的题是最简单的,然后除了提答以外的不那么水的题都是以前讲过.做过的,都比较好想到,但是我实现起来却有各种Bug,也完全不能在5h里AC...太弱了 [NOI2014]起床困难综合症 纯 ...

  9. Sublime text2 + cygwin编译C++

    1.安装cygwin2.安装sublime text23.将g++和gdb加入系统环境变量(windows系统)3.安装package control4.通过package control安装subl ...

  10. day19_生成器

    20180730 初次上传 20180731 更新,4.列表生成式,以及部分注释 #!/usr/bin/env python # -*- coding:utf-8 -*- # ************ ...