题目如下:

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

题目要求对分数进行处理,题目的关键在于求取最大公约数,最初我采用了循环出现超时,后来改用辗转相除法,解决了此问题。需要注意的是分子为负数的情况,为方便处理,我们把负数取绝对值,并且记录下符号,最后再输出。

辗转相除法如下:

给定数a、b,要求他们的最大公约数,用任意一个除以另一个,得到余数c,如果c=0,则说明除尽,除数就是最大公约数;如果c≠0,则用除数再去除以余数,如此循环下去,直至c=0,则除数就是最大公约数,直接说比较抽象,下面用例子说明。

设a=25,b=10,c为余数

①25/10,c=5≠0,令a=10,b=5。

②10/5,c=0,则b=5就是最大公约数。

求取最大公约数的代码如下:

long getMaxCommon(long a, long b){
long yu;
if(a == b) return a;
while(1){
yu = a % b;
if(yu == 0) return b;
a = b;
b = yu;
}
}

完整代码如下:

#include <iostream>
#include <stdio.h>
#include <vector> using namespace std; struct Ration{
long num;
long den; Ration(long _n, long _d){
num = _n;
den = _d;
} }; long getMaxCommon(long a, long b){
long yu;
if(a == b) return a;
while(1){
yu = a % b;
if(yu == 0) return b;
a = b;
b = yu;
}
} int main(){
int N;
long num,den;
long maxDen = -1;
cin >> N;
vector<Ration> rations;
for(int i = 0; i < N; i++){
scanf("%ld/%ld",&num,&den);
rations.push_back(Ration(num,den));
if(maxDen == -1){
maxDen = den;
}else{
// 找maxDen和当前的最小公倍数
if(den == maxDen) continue;
else if(maxDen > den){
if(maxDen % den == 0) continue;
}else{
if(den % maxDen == 0){
maxDen = den;
continue;
}
}
maxDen = maxDen * den;
}
}
num = 0;
for(int i = 0; i < N; i++){
num += rations[i].num * (maxDen / rations[i].den);
}
if(num == 0) {
printf("0\n");
return 0;
}
bool negative = num < 0;
if(negative) num = -num;
if(num >= maxDen){
long integer = num / maxDen;
long numerator = num % maxDen;
if(numerator == 0){
if(negative)
printf("-%ld\n",integer);
else
printf("%ld\n",integer);
return 0;
}
long common = getMaxCommon(numerator,maxDen);
if(negative){
printf("%ld -%ld/%ld\n",integer,numerator/common,maxDen / common);
}else{
printf("%ld %ld/%ld\n",integer,numerator/common,maxDen / common);
}
}else{
long common = getMaxCommon(num,maxDen);
if(negative)
printf("-%ld/%ld\n",num/common,maxDen/common);
else
printf("%ld/%ld\n",num/common,maxDen/common);
}
return 0;
}

1081. Rational Sum (20) -最大公约数的更多相关文章

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

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

  2. 1081. Rational Sum (20)

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

  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. PAT (Advanced Level) 1081. Rational Sum (20)

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

  6. PAT 1081 Rational Sum

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

  7. pat1081. Rational Sum (20)

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

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

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

  9. 1081 Rational Sum(20 分)

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

随机推荐

  1. Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)

    Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...

  2. hdu 5130(2014广州 圆与多边形相交模板)

    题意:一个很多个点p构成的多边形,pb <= pa * k时p所占区域与多边形相交面积 设p(x,y),       (x - xb)^2+(y - yb)^2 / (x - xa)^2+(y ...

  3. hdu3487 splay树

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. [Helvetic Coding Contest 2017 online mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...

  5. RabbitMQ-Spring AMQP

    上篇文章RabbitMQ基础入门学习了rabbitMQ一些基础的api,当然spring也在原生代码的基础上做了更多的封装,这篇文章就基于spring-rabbit,学习一下spring的实现. p. ...

  6. Android 自定义支持快速搜索筛选的选择控件(一)

    Android 自定义支持快速搜索筛选的选择控件 项目中遇到选择控件选项过多,需要快速查找匹配的情况. 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz ...

  7. c++ primer第15章这几个例子中的构造函数形式不太理解

    //向基类构造函数传递实参p491 class Bulk_item : public Item_base{ public: Bulk_item(,double disc_rate = 0.0): It ...

  8. 初探nginx

    nginx nginx是俄罗斯人写的轻量级http服务器,Nginx 以事件驱动的方式编写,有非常好的性能,同时也是一个非常高效的反向代理.负载均衡. Nginx 稳定性高,模块库丰富,配置灵活,系统 ...

  9. jieba库分词统计

    代码在github网站,https://github.com/chaigee/chaigee,中的z3.py文件 py.txt为团队中文简介文件 代码运行后词频统计使用xlwt库将数据发送到excel ...

  10. iOS开源加密相册Agony的实现(四)

    简介 虽然目前市面上有一些不错的加密相册App,但不是内置广告,就是对上传的张数有所限制.本文介绍了一个加密相册的制作过程,该加密相册将包括多密码(输入不同的密码即可访问不同的空间,可掩人耳目).Wi ...