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. 个人java框架 技术分析

    1.框架选型 spring-boot https://github.com/JeffLi1993/springboot-learning-example https://mp.weixin.qq.co ...

  2. sql 某字段存储另一个表的多个id值并以逗号分隔,现根据id去中文并拼接同样以逗号分隔

    首先介绍用到的两个函数 charindex(要查找的表达式1,表达式2),返回值为表达式1在表达式2中的下标,未找到则返回0.(sql的下标是从1开始的),例如 select charindex('s ...

  3. linux-文件流4种读取方式

    第二种方式 第三种 第四种: 小括号在管道符的右边开辟了两个子进程 大括号在管道符的右边开辟了一个子进程, export 用来导出子进程的 num 还可以借助外部文件进行 七步扩展:

  4. 《Linux内核设计与实现》 第八周读书笔记 第四章 进程调度

    20135307 张嘉琪 第八周读书笔记 第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统.只有 ...

  5. 使用github的感想

    github的仓库链接:https://github.com/liyan941016/test  github是一个基于git的代码托管平台,要想使用github第一步就要注册账户,然后要创建一个仓库 ...

  6. iOS开发CAAnimation详解

    Core Animation,即为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能.Core Anima ...

  7. Spring Boot, Java Config - No mapping found for HTTP request with URI [/…] in DispatcherServlet with name 'dispatcherServlet'

    Spring Boot 启用应用: error: No mapping found for HTTP request with URI [/…] in DispatcherServlet with n ...

  8. react + dva + ant架构后台管理系统(一)

    一.什么是dva dva是蚂蚁金服推出的一个单页应用框架,对 redux, react-router, redux-saga进行了上层封装,没有引入新的概念,但是极大的程度上提升了开发效率: 二.安装 ...

  9. 【转帖】互联网加密及OpenSSL介绍和简单使用

    转帖:https://mritd.me/2016/07/02/%E4%BA%92%E8%81%94%E7%BD%91%E5%8A%A0%E5%AF%86%E5%8F%8AOpenSSL%E4%BB%8 ...

  10. MES模块

    基础数据管理:产品模型.工厂模型.工艺模型 仓储管理 成本管理 绩效管理 看板管理 日志管理 设备管理:工装管理.设计器具管理.设备保养管理.设备备件管理.设备采集管理.设备点检管理.设备故障管理.设 ...