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++版 - ...
随机推荐
- c# 使用Expression 生成sql
使用Expression 生成sql update语句的时候遇到了个问题 ,Expression<Action<T>> la 这个委托里面老获取不到 引用类型的值,甚至 ...
- robotframework冷门关键字
1.Reload Page 模拟页面重载 2.Register Keyword To Run On Failure 参数: Keyword 描述: 当Selenium2Library类库关键字执行失败 ...
- adb命令 logcat日志抓取
一.logcat抓log方法:adb logcat命令,可以加条件过滤 1.安装SDK(参考android sdk环境安装) 2.使用数据线链接手机,在手机助手的sdcard中建立一个1.log的文件 ...
- php数组的快速排序
function quick($array){ if(count($array)<=1){ return $array; } $key=$array[0]; $right=array(); $l ...
- Kubernetes的包管理工具Helm的安装和使用
1.源码安装 [root@master ~]# wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.0-linux-amd64 ...
- android GPS: code should explicitly check to see if permission is available
转载的,感谢作者,由于我找了很久才找到这个解决方法,因此我自己再转一遍 原文链接 https://blog.csdn.net/qinwendou/article/details/77849048 if ...
- Python 函数与内置函数
1.函数的基本定义 def 函数名称(参数) 执行语句 return 返回值 def : 定义函数的关键字: 函数名称:顾名思义,就是函数的名字,可以用来调用函数,不能使用关键字来命名,做好是用这个函 ...
- oracle增加用户密码,cmd导入数据库
1.tomcat中sql语句 用户名 pdmis 密码pdmis create USER pdmis IDENTIFIED BY pdmis;grant create session to pdmis ...
- CSS 实现自适应正方形
在处理移动端页面时,我们有时会需要将banner图做成与屏幕等宽的正方形以获得最佳的体验效果,比如,商品详情页, 方法1.CSS3 vw单位 CSS3 中新增了一组相对于可视区域百分比的长度单位 vw ...
- (十一)Json文件配置
接上一节,新建一个项目:JsonConfigSample 依然添加Microsoft.AspNetCore.All 在项目下新建一个Class.json配置文件 { ", "Cla ...