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用中文索引
正在用的Neo4j是当前最新版:3.1.0,各种踩坑.说一下如何在Neo4j 3.1.0中使用中文索引.选用了IKAnalyzer做分词器. 1. 首先参考文章: https://segmentfau ...
- libevent的使用 32位 64位
前段时间一个项目需要用到高并发的服务,想着自己应该可以写一个,windwos 上使用完成端口实现,写完之后,测试感觉没什么问题,可是一上线各种各样的问题,初步怀疑是底层网路库的问题,于是决定更换网络库 ...
- 007-Java可变个数形参重载【数组和...】
重载方法时,可变个数形参的方法有两种方式 数组重载 ...重载 对两种方法,其实是一致的,示例如下: public class MethodArgsTest { //可变个数形参的格式:数据类型... ...
- Visual Studio Git代码管理环境部署
Visual Studio 2010 部署Git代码管理环境. 第一:首先做Git的安装和环境部署 1.下载并安装Git软件,在windows环境下的Git叫做“msysGit”,官网地址为https ...
- ASP.NET MVC easyUI-datagrid 的当前页合计and总合计
一.HTML代码 <table id="dg" class="easyui-datagrid" style="width: 100%; heig ...
- The linux command 之引用
[me@linuxbox ~]$ echo this is a test this is a test shell 会对echo进行单词分割(word splitting)去除多余的空白. [me@l ...
- openwrt_ipsec_function.sh 分析
#!/bin/sh # # Copyright (C) Vitaly Protsko <villy@sft.ru> errno= # get_fieldval gate src " ...
- LR调试脚本的时候报错Error -27796:(已解决)
LR调试bbs脚本的时候报错: 1.Error -27796: Failed to connect to server "192.168.211.128:80": [10060] ...
- Python: 生成器与迭代 generators and iteration
https://eastlakeside.gitbooks.io/interpy-zh/content/Generators/ 文章不是非常好 1,三个概念 可迭代对象 iterable, 迭代器 i ...
- 区别 |峰度(Kurtosis)和偏度(Skewness)
峰度(Kurtosis) 定义 峰度又称峰态系数,表征概率密度分布曲线在平均值处峰值高低的特征数,即是描述总体中所有取值分布形态陡缓程度的统计量.直观看来,峰度反映了峰部的尖度.这个统计量需要与正 ...