PAT1081:Rational Sum
1081. Rational Sum (20)
Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.
Input Specification:
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.
Output Specification:
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.
Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
Sample Output 1:
3 1/3
Sample Input 2:
2
4/3 2/3
Sample Output 2:
2
Sample Input 3:
3
1/3 -1/6 1/8
Sample Output 3:
7/24 思路 分子相加的运算。
1.辗转相除法求分子分母的最大公约数
2.两分数相加后要化简,不然容易在计算时产生溢出。
3.输出需要特别注意的格式:
1)在整数不为0的情况下,分数为0则只输出整数。
2)在整数为0的情况下,分数不为0则只输出分数。
3)二者都为0直接输出一个0。
4)二者都不为0按题目要求的标准格式输出。
4.关于分母为0的情况,题目测试用例好像并未考虑,暂不做处理。 代码
#include<iostream>
using namespace std;
typedef long long ll; ll gcd(ll a,ll b) //求最大公约数
{
return b == 0?abs(a):gcd(b,a % b);
}
int main()
{
ll N,a,b,gvalue,suma,sumb;
while( cin >> N)
{
suma = 0,sumb = 1;
for(int i = 0;i < N;i++)
{
scanf("%lld/%lld",&a,&b);
gvalue = gcd(a,b);
//约分
a /= gvalue;
b /= gvalue;
//分数求公倍数相加
suma = a * sumb + b * suma;
sumb = b * sumb;
//分子和约分
gvalue = gcd(suma,sumb);
suma /= gvalue;
sumb /= gvalue;
}
ll integer = suma / sumb;
ll numerator = suma - integer * sumb;
if(integer != 0)
{
cout << integer;
if(numerator != 0)
{
cout << " ";
printf("%lld/%lld",numerator,sumb);
}
}
else
{
if(numerator != 0)
{
printf("%lld/%lld",numerator,sumb);
}
else
cout << 0;
}
cout << endl;
}
}
PAT1081:Rational Sum的更多相关文章
- pat1081. Rational Sum (20)
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- PAT Rational Sum
Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- PAT_A1081#Rational Sum
Source: PAT A1081 Rational Sum (20 分) Description: Given N rational numbers in the form numerator/de ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- A1081. Rational Sum
Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...
- Twitter OA prepare: Rational Sum
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
随机推荐
- C语言函数strstr()分析及实现
原型:char *strstr(const char *str1, const char *str2); #include<string.h> 找出str2字符串在str1字符串中第一次出 ...
- 【Java编程】Java学习笔记<二>
种访问权限,而类的访问控制级别只有public和缺省的,当为public时,可以被任何包的其他类访问,当为缺省时,只能被同一包的其他类访问.如果类自身对另一个类是不可见的,即使类的成员声明为publi ...
- Android驱动中的Kconfig文件与Makefile文件
内核源码树的目录下都有两个文档Kconfig(2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源文 ...
- Hadoop 的 TotalOrderPartitioner
Partition所处的位置 Partition位置 Partition主要作用就是将map的结果发送到相应的reduce.这就对partition有两个要求: 1)均衡负载,尽量的将工作均匀的分配给 ...
- RTMPdump(libRTMP) 源代码分析 8: 发送消息(Message)
===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...
- HI258摄像头旋转配置问题
{0x28, 0x04}, //Full row start y-flip {0x29, 0x01}, //Pre1 row start no-flip {0x2a, 0x02}, //Pre1 r ...
- Zeroc Ice开发环境搭建
搭建Ice环境 1. Linux(推荐,更接近真实生产环境) 2. Windows(方便学习开发) 下载安装包:https://zeroc.com/downloads (百度网盘链接:http ...
- 《老罗的Android之旅》导读PPT
虽然好几个月没更新博客了,但是老罗一直有在准备可以分享的东西的.除了早前在微博分享Android4.2相关技术之外,这次还特意准备了13个PPT,总结之前所研究过的东西.内容从Android组件设计思 ...
- 恶补web之一:html学习(1)
发现以前欠下的web知识太多鸟,只有重头开始好好学吧,恶补第一站就是html知识啦! html指的是超文本标记语言,它不是编程语言,而是一种标记语言;标记语言是一套标记标签(markup tag),h ...
- python字符串27种常见的方法
如有字符串 mystr = 'hello world itcast and itcastcpp' ,以下是常见的操作: <1>find 检测 str 是否包含在 mystr中,如果是返回开 ...