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++版 - ...
随机推荐
- MySQL 08章_数据库设计
一. 关系模型与对象模型之间的对应关系 序号 关系模型:数据库 对象模型:java程序 1 数据表table 实体entity:特殊的java类 2 字段field 属性attribute/字段fie ...
- java——有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
package java_day10; /* * 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? */ public class Demo04 { public stat ...
- Spark三种部署方式
- Windows中的"簇"和Linux中的"块"是对应的
扇区是对硬盘而言,块是对文件系统而言. 簇”又称为“分配单元” ,文件系统是操作系统与驱动器之间的接口,当操作系统请求从硬盘里读取一个文件时,会请求相应的文件系统(FAT 16/32/NTFS)打开文 ...
- 十一. for of
const fruits = ['Apple','Banana','Orange','Mango']; es5: 可读性差 for(let i=0; i < fruits.length; i + ...
- Idea中创建maven骨架的命令
如下:通过命令化在Idea中创建骨架成功后,以后项目直接引用导入骨架直接在依赖框架上面进行相关模块开发: 1.mvn archetype:create-from-project 2.mvn clean ...
- 阿里云应用上边缘云解决方案助力互联网All in Cloud
九月末的杭州因为一场云栖大会变得格外火热. 9月25日,吸引全球目光的2019杭州云栖大会如期开幕.20000平米的展区集结数百家企业,为数万名开发者带来了一场前沿科技的饕餮盛宴. 如同往年一样,位于 ...
- iOS扩展Extension之Today
1.简介 扩展(Extension)是iOS 8中引入的一个新特性.扩展让app之间的数据交互成为可能.在iOS 8系统之前,每一个app在物理上都是彼此独立的,app之间不能互访彼此的私有数据.而在 ...
- JSON关联属性转换异常
问题:FastJSON在转换对象过程中,该对象还有关联属性,该属性还是一个对象,就出现栈溢出异常,会报一下错误,解决办法:在该属性类的一边加上@JSONField(serialize=false);有 ...
- uoj#311 【UNR #2】积劳成疾
题目 考虑直接顺着从\(1\)填数填到\(n\)发现这是在胡扯 所以考虑一些奇诡的东西,譬如最后的答案长什么样子 显然某一种方案的贡献是一个\(\prod_{i=1}^nw_i^{t_i}\)状物,\ ...