PAT甲级——【牛客练习题100】
题目描述
- 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】的更多相关文章
- PAT甲级——【牛客练习题1002】
题目描述 Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chi ...
- PAT甲级训练刷题代码记录
刷题链接:https://www.patest.cn/contests/pat-a-practise 1001 #include <iostream> #include <stdio ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- 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 ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- PAT甲级终结——心得总结
首先报喜一波 第一次考,满分,4道题总共花了2个小时做完,一部分是题简单的原因,一部分也是自己三刷了PAT的心血吧. 刷PAT的经验 神指导: 胡凡-<算法笔记> 神助攻:柳婼的博客,百度 ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...
随机推荐
- Neo4j使用简单例子
Neo4j Versions Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip th ...
- Ajax.BeginForm 在 Chrome下的问题
项目背景:MVC4 代码: @using (Ajax.BeginForm("Index", "GoingMeter", new AjaxOptions { On ...
- 无法解析的外部符号 jpeg_std_error
1>dlib.lib(png_loader.obj) : error LNK2001: 无法解析的外部符号 png_set_sig_bytes 1>dlib.lib(png_loader. ...
- 清空资源管理器访问过FTP的账号、密码
修改注册表,删除HKEY_CURRENT_USER\SOFTWARE\Microsoft\FTP\Accounts下相对应的项即可,即为xxx.xxx.xxx.xxx项. 如下图所示:
- JS鼠标经过
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>菜鸟 ...
- Python 正整数相加其余忽略
从键盘上输入若干数值,对其中的正整数求和,非正整数(负整数,实数或其他符号)忽略,这个过程一直到输入“#”结束. i = 0while True: m = input("请输入一个数:&qu ...
- python 基本常用数据类型
#字典类型 result={1:2222,2:2221111}; result.items();#获取字典中所有元素 result.keys();#获取字典的key result.values();# ...
- 括号匹配——cf1095E
正解应该是求后缀和前缀 但是多情况讨论好像也能过.. 大概分为: 首先排除不能改的情况 1.改左括号 2.改右括号 /* 将一个位置的括号反过来,使原序列变成合法序列 */ #include<b ...
- android—退出应用程序
在android系统中,当你点击返回按钮时,会默认调用finish方法(还是destroy方法,记不太清楚),这样你就能退出当前Activity.注意是当前Activity,不是应用程序,因为如果这个 ...
- centos7 创建桌面快捷方式(chrome,eclipse)
在将eclipse-SDK-3.7.2-Linux-gtk.tar.gz解压到某个目录下之后,命令行进行如下编辑 vi /usr/share/applications/eclipse.desktop ...