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部分.语言是 ...
随机推荐
- 面试系列38 分库分表之后,id主键如何处理?
(1)数据库自增id 这个就是说你的系统里每次得到一个id,都是往一个库的一个表里插入一条没什么业务含义的数据,然后获取一个数据库自增的一个id.拿到这个id之后再往对应的分库分表里去写入. 这个方案 ...
- 流计算与Hadoop
- gcc 4步编译过程
一. gcc编译过程 1. 预处理: 主要进行宏替换以及头文件的展开 gcc -E *.c -o *.i 2. 编译::编译生成汇编文件,会检查语法错误 gcc -S *.i ...
- 牛客CSP-S提高组赛前集训营1
牛客CSP-S提高组赛前集训营1 比赛链接 官方题解 before:T1观察+结论题,T2树形Dp,可以换根或up&down,T3正解妙,转化为图上问题.题目质量不错,但数据太水了~. A-仓 ...
- 跟我一起使用Vue.js + Docker 部署项目
本文学习自:https://juejin.im/post/5bee5ddde51d457b8a33938c 项目环境是在ubuntu下,记得要在root目录下,不然安装vue会报错 npm insta ...
- PHP算法之删除最外层的括号
有效括号字符串为空 ("")."(" + A + ")" 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接.例如 ...
- eclipse导出说明文档
选中项目--右键--Export--Java--Javadoc—Finish 1.为程序添加文档注释 2.选中项目--右键Export--Java--Javadoc--next, 3.next--在V ...
- soj116 快乐串
题意:定义一个串是k-happy的:对于所有的Ai,都有Aj(j!=i),使得|Ai-Aj|<=k. 问使得原串至少存在一个长度>=m的连续子串是k-happy的最小的k? 标程: #in ...
- vs2013x64&&qt5.7.1编译osg3.4.0&&osgEarth2.7
此文仅备忘: 1.安装VS2013_Cn_Ult 2.安装qt-opensource-windows-x86-msvc2013_64-5.7.1 设置环境变量QTDIR,并将其bin加入到path中. ...
- scala中的闭包简单使用
object Closure { /** * scala中的闭包 * 函数在变量不处于其有效作用域内,还能够对变量进行访问 * * @param args */ def main(args: Arra ...