题目

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

题目分析

给出N有理数,格式为分子/分母,若为负,则负号一定在分子前。求N个有理数的和

解题思路

  1. 输入分子、分母,化简
  2. 计算累加分数和与下一个分数和,化简
  3. 累加完成后,假分数转换为真分数,打印整数部分和分式部分

易错点

  1. 若和为0,要输出“0”(否则测试点4错误)

知识点

  1. long long类型的数据输入/输出

    输入:scanf("%lld",&n);

    输出:printf("%lld",n);

Code

Code 01

#include <iostream>
using namespace std;
// 求公约数
int gcd(long long a, long long b) {
return b==0?abs(a):gcd(b, a%b);
}
// 化简分式
void reduction(long long &a, long long &b) {
int gcdvalue = gcd(a,b);
a /= gcdvalue;
b /= gcdvalue;
}
int main(int argc,char * argv[]) {
long long n,a,b,suma=0,sumb=1,gcdvalue;
scanf("%lld",&n);
for(int i=0; i<n; i++) {
scanf("%lld/%lld",&a,&b);
reduction(a,b);
suma=a*sumb+suma*b;
sumb=b*sumb;
reduction(suma,sumb);
}
long long itg = suma/sumb;
suma = suma-(sumb*itg);
if(itg!=0) {
printf("%lld",itg);
if(suma!=0)printf(" ");
}
if(suma!=0)printf("%lld/%lld",suma,sumb);
if(itg==0&&suma==0)printf("0");
return 0;
}

PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]的更多相关文章

  1. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

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

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

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

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

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

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

  5. 1081. Rational Sum (20)

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

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

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

  7. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...

  8. PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]

    题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...

  9. PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]

    题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...

随机推荐

  1. css把图片方框变为圆角

    border-radius:10px; 多少就设多少像素,个人需求.

  2. SPOJ ANARC05H 计数DP

    给定一个数字串,问有多少种拆分方法,题目所谓的拆分,就是分成若干个子块,每个块的和 即为各个数字相加,当前块的和一定要小于等于后面的块的和 比如1117  就有这些[1-117], [1-1-17], ...

  3. junit基础学习之-测试controller层(2)

    准备工作: eclipse本身带有junit4,可以直接build path,加入junit. 连接数据库的配置文件需要修改,之前的文件是采用properties+xml文件的形式,但是在测试的时候因 ...

  4. 第八篇Django分页

    Django分页 1.复杂版 data = [] , ): tmp = {"id": i, "name": "alex-{}".format ...

  5. Node.js的启动和调试方式

    通过node命令启动 node server/bin/www webstorm配置启动入口 pm2 全局安装:cnpm i pm2 -g 检查版本:pm2 -v 启动:cd 项目目录 pm2 star ...

  6. SpringBoot学习(四)——配置文件占位符

    RandomValuePropertySource:配置文件中可以使用随机数 ${Random.value}  ${random.int}, ${random.long}, ${random.int( ...

  7. 吴裕雄--天生自然 JAVASCRIPT开发学习: Break 和 Continue 语句

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. POJ - 3977 Subset(二分+折半枚举)

    题意:有一个N(N <= 35)个数的集合,每个数的绝对值小于等于1015,找一个非空子集,使该子集中所有元素的和的绝对值最小,若有多个,则输出个数最小的那个. 分析: 1.将集合中的元素分成两 ...

  9. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

  10. Charles下载与破解方法

    文章参考:charles4.2下载与破解方法以及配置https 1.Charles官网下载 地址:https://www.charlesproxy.com/latest-release/downloa ...