PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
Input Specification:
Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2
. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.
Output Specification:
For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result
. Notice that all the rational numbers must be in their simplest form k a/b
, where k
is the integer part, and a/b
is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf
as the result. It is guaranteed that all the output integers are in the range of long int.
Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
就一句话,细节很重要!
#include <iostream>
using namespace std;
long long dv, res1, res2;
long long DIV(long long a, long long b)
{
if (b == )
return abs(a);
return DIV(b, a%b);
}
void print(long long a1, long long b1, long long a2, long long b2, char c)
{
if (a1 == )
printf("%d %c ", , c);
else
{
printf("%s", a1 > ? "" : "(");
dv = DIV(a1, b1);
a1 /= dv;
b1 /= dv; if (a1 / b1 != )
printf("%d", a1 / b1);
if (a1 - b1 * (a1 / b1) != )
printf("%s%d/%d", a1 / b1 != ? " " : "", a1 / b1 != ? abs(a1 - b1 * (a1 / b1)) : a1, b1);
printf("%s %c ", a1 > ? "" : ")", c);
} if(a2 == )
printf("%d %s ", , "=");
else
{
printf("%s", a2 > ? "" : "(");
dv = DIV(a2, b2);
a2 /= dv;
b2 /= dv;
if (a2 / b2 != )
printf("%d", a2 / b2);
if (a2 - b2 * (a2 / b2) != )
printf("%s%d/%d", a2 / b2 != ? " " : "", a2 / b2 != ? abs(a2 - b2 * (a2 / b2)) : a2, b2);
printf("%s %s ", a2 > ? "" : ")", "=");
} if (res1 == )
{
printf("%d\n",);
return;
}
else if (res2 == )
{
printf("Inf\n");
return;
}
printf("%s", res1 > ? "" : "(");
dv = DIV(res1, res2);
res1 /= dv;
res2 /= dv;
if (res1 / res2 != )
printf("%d", res1 / res2);
if (res1 - res2 * (res1 / res2) != )
printf("%s%d/%d", res1 / res2 != ? " " : "", res1 / res2 != ? abs(res1 - res2 * (res1 / res2)) : res1, res2);
printf("%s\n", res1 > ? "" : ")");
}
int main()
{
char c;
long long a1, b1, a2, b2;
cin >> a1 >> c >> b1 >> a2 >> c >> b2;
// +
res1 = a1 * b2 + a2 * b1;
res2 = b1 * b2;
print(a1, b1, a2, b2, '+');
// -
res1 = a1 * b2 - a2 * b1;
res2 = b1 * b2;
print(a1, b1, a2, b2, '-');
// *
res1 = a1 * a2;
res2 = b1 * b2;
print(a1, b1, a2, b2, '*');
// /
res1 = a2 > ? a1 * b2 : a1 * b2*-;
res2 = b1 * abs(a2);
print(a1, b1, a2, b2, '/');
return ;
}
PAT甲级——A1088 Rational Arithmetic的更多相关文章
- A1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- PAT甲级——A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题分类汇编——计算
本文为PAT甲级分类汇编系列文章. 计算类,指以数学运算为主或为背景的题. 题号 标题 分数 大意 1058 A+B in Hogwarts 20 特殊进制加法 1059 Prime Factors ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
随机推荐
- 【数论】[SDOI2010]古代猪文
大概就是求这个: $$G^\sum_{k|N} C_{n}^{k}$$ 显然只要把后面的$\sum_{k|N}C_{n}^{k}$求出来就好了 几个要用的定理: 欧拉定理的推论:(a和n互质) $$a ...
- iptables 命令大全
1.连续端口配置 iptables可以方便的配置多个端口.其中根据端口的连续性,又可分为连续端口配置和不连续端口配置. 如: -A INPUT -p tcp –dport 21:25 -j DROP/ ...
- (1)python tkinter-窗体
1.导入自带的包名 import tkinter 2.创建一个窗体对象 form=Tkinter.Tk() 3.显示窗体(这句应该是所有的设置部署完最后执行的一句代码) form.mainloop() ...
- 自签https证书2(适配新版chrome,不会显示“不安全”)
上一篇博文中介绍了自签https的方法,但是在新版的chrome中会出现这么一个问题:自签ca可以识别,但是证书仍然会判断不安全.为了解决这个问题,博主特地又查了好多资料,终于找到了解决方案. 当然, ...
- Mac 上的 redis
Mac下添加redis的环境变量: echo 'export PATH="/usr/local/opt/redis@3.2/bin:$PATH"' >> ~/.bash ...
- <数据分析>初级入门
1.何为数据分析? 数据分析是指用适当的统计方法对收集来的大量数据进行分析,将它们加以汇总和理解消化,以求最大化地开发数据的功能,发挥数据的作用. 直接的理解:提炼杂乱无章的数据背后的信息,总结出研究 ...
- HYNB Round 15: PKU Campus 2019
HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归 ...
- Activiti常用类介绍
为什么要使用工作流? 传统的设计在流程发生变化时的弊端: 1. 流程相关的属性和业务对象的属性,都放到了业务对象中. 2. 流程相关的逻辑和业务逻辑,都放到的业务逻辑中 常用类 ProcessEngi ...
- Python 字符串切片(slice)
切片操作(slice)可以从一个字符串中获取子字符串(字符串的一部分).我们使用一对方括号.起始偏移量start.终止偏移量end 以及可选的步长step 来定义一个分片. 格式: [start:en ...
- LVS/Nginx/HAProxy负载均衡器的对比分析
转自:http://www.blogjava.net/ivanwan/archive/2013/12/25/408014.html LVS的特点是: 抗负载能力强.是工作在网络4层之上仅作分发之用,没 ...