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<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Fraction{
ll up,dowm;
}; ll gcd(ll a,ll b){
return b == ? a : gcd(b,a%b);
} Fraction reduction(Fraction result){
if(result.dowm < ){
result.up = - result.up;
result.dowm = - result.dowm;
}
if(result.up == ){
result.dowm = ;
}else{
int d = gcd(abs(result.dowm),result.up);
result.dowm /= d;
result.up /= d;
}
return result;
} Fraction add(Fraction f1,Fraction f2){
Fraction result;
result.up = f1.dowm*f2.up + f2.dowm*f1.up;
result.dowm = f1.dowm*f2.dowm;
return reduction(result);
} void showResult(Fraction r){
reduction(r);
if(r.dowm == ) printf("%lld",r.up);
else if(abs(r.up) > abs(r.dowm)){
printf("%lld %lld/%lld",r.up/r.dowm,r.up%r.dowm,r.dowm);
}else{
printf("%lld/%lld",r.up,r.dowm);
}
} int main(){
int n;
scanf("%d",&n);
Fraction sum,temp;
sum.up = , sum.dowm = ;
for(int i = ; i < n; i++){
scanf("%lld/%lld",&temp.up,&temp.dowm);
sum = add(sum,temp);
}
showResult(sum);
return ;
}

1081 Rational Sum(20 分)的更多相关文章

  1. 【PAT甲级】1081 Rational Sum (20 分)

    题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...

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

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

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

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

  4. 1081. Rational Sum (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...

  5. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  6. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  7. PAT 1081 Rational Sum

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

  8. pat1081. Rational Sum (20)

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

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

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

随机推荐

  1. jQuery移动光标改变图像

    脚本代码移动光标改变图像是一款让你通过移动光标显示和调整多个图像或其他方法来触发. 代码:http://www.huiyi8.com/sc/10628.html

  2. 淘宝双十一页面(Flexible)demo

    下面的代码是看了大漠 使用Flexible实现手淘H5页面的终端适配 做的一个demo. <!DOCTYPE html> <html lang="en" ng-a ...

  3. linux应用之nginx的源码安装及配置(centos)

    1.准备工作选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL.Nginx是C写的,需要用GCC编译:Nginx的 ...

  4. VC++动态链接库(DLL)编程深入浅出(转帖:基础班)

    1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了“无库-静 ...

  5. Spring笔记03(Spring创建对象的三种方式)

    1.创建对象的三种方式和bean的生命周期的验证: Animal接口代码: package cn.pb.dao; /** * 动物接口 */ public interface Animal { //吃 ...

  6. Eclipse&nbsp;Helios(3.6.2)下载地址

    Eclipse Helios(3.6.2)下载地址   鉴于有些插件最高只能支持到指定的eclipse 3.6版本,以此收集3.6下载地址 Eclipse Helios (v3.6.2) Eclips ...

  7. 《精通Spring4.X企业应用开发实战》读后感第三章

  8. hdu3047 Zjnu Stadium (并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. <c和指针>学习笔记4之字符串

    1 字符串基础 NUL字节是字符串的终止符,但它本身并不是字符串的一部分,所以字符串的长度并不包括NUL字节. (1)字符串长度 size_t strlen(char const * string)注 ...

  10. windows64下安装MySQLdb连接数据库

    MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 1.首先要下载:http://www.codegood.com/downloads,由于我的python是2 ...