题目描述

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

输入描述:

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.

输出描述:

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.

输入例子:

5
2/5 4/15 1/30 -2/60 8/3

输出例子:

3 1/3
 #include <iostream>
#include <vector>
#include <math.h> using namespace std; long int gcd(long int a, long int b)
{
if(b==) return a;
else return gcd(b,a%b);
} int main()
{
double N;
cin>>N;
long int Inter = ;//整数
long int resa = ;//答案分子
long int resb = ;//答案分母 for(int i=;i<N;++i)
{
long int a = ;//输入分子
long int b = ;//输入分母
char c;
cin >> a >> c >> b; int f = ;
if(a<)
{
a = a*-;
f = -;
}
Inter += a/b; //简化
a = a-b*(a/b); long int Div = ;//最大公约数
long int Mul = ;//最小公倍数
//化简输入的分数
Div = gcd(b,a);
a = a/Div;
b = b/Div; //求最大公倍数
if(resb > b)
{
Div = gcd(resb,b);
Mul = resb / Div * b;
}
else
{
Div = gcd(b, resb);
Mul = b / Div * resb;
}
//相加
resa = resa * (Mul/resb) + f * a * (Mul/b);
resb = Mul;
//化简有理数
Inter += resa / resb;
resa = resa - (resa / resb)*resb; //化简最简分数
Div = gcd(resb, fabs(resa));
resa = resa/Div;
resb = resb/Div;
}
if(Inter== && resa==)
cout << << endl;
else if(Inter != && resa == )
cout << Inter << endl;
else if(Inter == && resa != )
cout << resa << "/" << resb << endl;
else
cout << Inter << " " << resa << "/" << resb << endl; return ;
}
												

PAT甲级——【牛客练习题100】的更多相关文章

  1. PAT甲级——【牛客练习题1002】

    题目描述 Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chi ...

  2. PAT甲级训练刷题代码记录

    刷题链接:https://www.patest.cn/contests/pat-a-practise 1001 #include <iostream> #include <stdio ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  5. PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名

    题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...

  6. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  7. PAT甲级终结——心得总结

    首先报喜一波 第一次考,满分,4道题总共花了2个小时做完,一部分是题简单的原因,一部分也是自己三刷了PAT的心血吧. 刷PAT的经验 神指导: 胡凡-<算法笔记> 神助攻:柳婼的博客,百度 ...

  8. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  9. C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...

随机推荐

  1. Codeforces 479【B】div3

    题目链接: http://codeforces.com/problemset/problem/977/B 题意:字符串,找固定长度为2的重复子串出现次数最多的. 题解:我暴力做的.暴力出奇迹. #in ...

  2. maven javaProject打包发布成服务

    1,现在pom.xml中添加打包所需要的jar包. <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  3. java——有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

    package java_day10; /* * 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? */ public class Demo04 { public stat ...

  4. ie8以下不兼容h5新标签的解决方法

    HTML5新添了一些语义化标签,他们能让代码语义化更直观易懂,有利于SEO优化.但是此HTML5新标签在IE6/IE7/IE8上并不能识别,需要进行JavaScript处理. 解决思路就是用js创建h ...

  5. OC开发系列-@property和@synthesize

    property和synthesize 创建一个Person类.提供成员属性的_age和_height的setter和getter方法. #import <Foundation/Foundati ...

  6. 2019-8-31-dotnet-非泛型-类型-System.Collections.IEnumerable-不能与类型实参一起使用

    title author date CreateTime categories dotnet 非泛型 类型 System.Collections.IEnumerable 不能与类型实参一起使用 lin ...

  7. JS事件 鼠标单击事件( onclick )通常与按钮一起使用。onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件。同时onclick事件调用的程序块就会被执行

    鼠标单击事件( onclick ) onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件.同时onclick事件调用的程序块就会被执行,通常与按钮一起使用. 比如,我们单击按钮时,触发  ...

  8. CSIC_716_20191029【人脸打分系统】

    今日内容: 1.调用百度的AI接口,完成人脸图像打分( 敷衍) 2.完成系统内置时间的打印 3.将上述两段代码生成可执行文件 ------------------------------------- ...

  9. 背包dp+打表处理——cf999F

    考虑每种c都是可以独立进行计算的,所以这题的答案等价于每种c的最优解之和 计算每种c的最优解:把问题转化成求出每种c的最大值,再转化成i个人分j张卡片的最大收益 dp[i,j]表示i个人分j张卡片的最 ...

  10. python和go对比字符串的链式处理

    一.什么是链式处理 对数据的操作进行多步骤的处理称为链式处理,链式处理器是一种常见的编程设计,链式处理的开发思想将数据和操作拆分,解耦,让开发者可以根据自己的技术优势和需求,进行系统开发,同时将自己的 ...