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
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct{
long long up, down;
}fra;
long long gcd(long long a, long long b){
a = abs(a);
b = abs(b);
if(b == )
return a;
else return gcd(b, a % b);
}
fra cacul(fra a, fra b){
fra temp;
temp.down = a.down * b.down;
temp.up = a.down * b.up + b.down * a.up;
long long fact = gcd(temp.up, temp.down);
temp.down = temp.down / fact;
temp.up = temp.up / fact;
return temp;
}
int main(){
int N;
fra re = {,}, temp = {, };
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%lld/%lld", &temp.up, &temp.down);
re = cacul(re, temp);
}
if(re.up == ){
printf("");
}else if(abs(re.up) == abs(re.down)){
printf("%lld", re.up / re.down);
}else if(abs(re.up) > abs(re.down)){
if(re.up % re.down == )
printf("%d", re.up / re.down);
else
printf("%lld %lld/%lld", re.up / re.down, re.up % re.down, re.down);
}else{
printf("%lld/%lld", re.up, re.down);
}
cin >> N;
return ;
}

总结:

1、分数运算化简:化简时分子分母同除最大公因数。 输出时考虑:分子为0时直接输出0;分子>=分母时(用绝对值比较,是>=而非>),可以整除则输出整数,否则输出代分数(4/1直接输出4);

2、gcd函数:

int gcd(int a, int b){ 
if(b == )
return a;
else return gcd(b, a % b);
}

A1081. Rational Sum的更多相关文章

  1. PAT甲级——A1081 Rational Sum

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

  2. PAT_A1081#Rational Sum

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

  3. PAT1081:Rational Sum

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

  4. PAT 1081 Rational Sum

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

  5. PAT Rational Sum

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

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

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

  7. pat1081. Rational Sum (20)

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

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

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

  9. 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 ...

随机推荐

  1. 关于Prometheus运维实践项目

    关于Promethues运维实践项目 1. 什么是Prometheus运维实践项目 ​ 是什么 ​ Prometheus,普罗米修斯,是古希腊神话中为人间带来火种的神. ​ Prometheus运维实 ...

  2. sigar开发(java)

    下载sigar,地址:https://yunpan.cn/cBEWbEfdAm98f (提取码:f765) 可以收集的信息 CPU信息:包括基本信息(vendor.model.mhz.cacheSiz ...

  3. C程序设计实践教学提示

    实践教学要点:实验重心应放在实验室之外,重在实验准备 对实验题目的分析是一个复杂的工作,很发时间的,如全部放在实验上机时来完成,是不现实的.(特别是后面实验的难度增大,或实验代码增多的情况下),而且, ...

  4. 用户模拟+spec

    用户模拟:用户模拟的对象分别为小学二年级学生,学生父亲以及小学教师. 名字 孔小颖 性别 男 职业 小学生 收入 来自父母给予 知识层次和能力 小学二年级 生活/工作情况 在学校上课,数学成绩较差,放 ...

  5. A Survey of Machine Learning Techniques Applied to Software Defined Networking (SDN): Research Issues and Challenges

    将机器学习用到SDN中的综述:研究的问题和挑战 从流量分类.路由优化.服务质量(Qos)/体验质量(QoE)预测.资源管理和安全性的角度,回顾了机器学习算法如何应用于SDN领域. 相关知识 在SDN中 ...

  6. JavaScript 字符串与数组互转,并保持数据去重、排序功能

    var valueArr = new Array(); if( $("input[name='type']").val() != ""){ valueArr = ...

  7. C/C++关键字 new/delete和malloc/free

    基本上new/delete来自于C++,作为对对象的创建.因此在使用new创建对象时候new会调用对象的构造函数,同样delete会调用对象的析构函数释放对象.而malloc/free操作的是直接的内 ...

  8. charCodeAt与fromCharCode

    charCodeAt() 方法可返回指定位置的字符的 Unicode 编码 这个返回值是 0 - 65535 之间的整数. stringObject.charCodeAt(index) /* a-z  ...

  9. Docker安装指定版本

    今天新增一个Docker服务器,Docker安装顺利,启动hello-world测试的时候却出现了问题: $ docker run hello-worldUnable to find image 'h ...

  10. Windows 聆听 简单使用体验

    1. 点击windows 按键 输入语音 按照操作 选择语音 并且读出那一段话. 2. 可以将windows 语音识别 添加到开始面板 3. 使用时 点击 该图标,然后点击麦克风按钮 聆听效果如图示 ...