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. [UWP 自定义控件]了解模板化控件(2):模仿ContentControl

    ContentControl是最简单的TemplatedControl,而且它在UWP出场频率很高.ContentControl和Panel是VisualTree的基础,可以说几乎所有VisualTr ...

  2. haproxy反向代理环境部署(http和https代理)

    操作背景:前方有一台haproxy代理机器(115.100.120.57/192.168.1.7),后方两台realserver机器(192.168.1.150.192.168.1.151,没有公网i ...

  3. springboot undertow替换tomcat方式

    版权声明: https://blog.csdn.net/weixin_38187317/article/details/81532560说明        undertow,jetty和tomcat可 ...

  4. 【2015 软件工程 个人项目 PJ1】四则运算题目生成程序

    1.开发时间预估 PSP2.1 Personal Software Process Stages Time Planning 计划 · Estimate · 估计这个任务需要多少时间 2day Dev ...

  5. 01springboot快速入门

    SpringBoot快速入门 springboot的宗旨是习惯大于配置,所以spring里面大量使用了默认的配置来简化spring的配置.spring Boot的主要优点: 为所有Spring开发者更 ...

  6. MySQL基础~~表结构操作

    登录数据库服务器 mysql -h127.0.0.1 -uroot -p123456 创建数据库 create database test; 显示所有数据库 show databases; 指定要操作 ...

  7. 修复PLSQL Developer 与 Office 2010的集成导出Excel 功能

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.htm]"PerceivedType"="text&qu ...

  8. mysql问题处理积累

    1.mysql errors:message from server: "Host 'xxx' is blocked because of many connection errors; u ...

  9. YII2十三大特性2

    第十三 场景(scenario)的使用 例如:有三个场景,分别为创建,更新,确认回款 首先,定义所有的场景,及规则,如下所示: <?php namespace core\models; use ...

  10. js輸出

    js訪問html的某個元素,使用document.getElementByID(); document.write()僅僅向文檔輸出內容,如果在頁面已經加載后輸出,原來頁面的內容會被覆蓋. docum ...