✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1088 Rational Arithmetic (pintia.cn)
🔑中文翻译:有理数运算
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪
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 formk a/b, wherekis the integer part, anda/bis 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, outputInfas the result. It is guaranteed that all the output integers are in the range of long int.Sample Input 1:
2/3 -4/2
- 1
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)
- 1
- 2
- 3
- 4
Sample Input 2:
5/3 0/6
- 1
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
- 1
- 2
- 3
- 4
给定两个有理数,你的任务是实现基本算术,即计算它们的和,差,积和商。
输入共一行,以 a1/b1 a2/b2 的形式给出两个有理数。
分子和分母都在 long int 范围内,如果存在负号,则只能出现在分子前面,分母保证为非零数字。
这道题就是模拟分数的加减乘除运算即可,假设有两个分数 a/b 和 c/d ,则运算公式分别如下:
【加法】 a = a ∗ d + c ∗ b a = a * d + c * b a=a∗d+c∗b, b = b ∗ d b = b * d b=b∗d
【减法】 a = a ∗ d − c ∗ b a = a * d - c * b a=a∗d−c∗b, b = b ∗ d b = b * d b=b∗d
【乘法】 a = a ∗ c a = a * c a=a∗c, b = b ∗ d b = b * d b=b∗d
【除法】
a
=
a
∗
d
a = a * d
a=a∗d,
b
=
b
∗
c
b = b * c
b=b∗c(另外,如果 c 为 0 ,则直接输出 Inf )
输出分数的时候需要先对分子分母进行化简,求它们的最大公约数。同时,要注意负数情况,如果分母为负数,则需要转移到分子上再输出,且输出负数时要加括号。
#include
using namespace std;
typedef long long LL;
//求最大公约数
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
//打印运算结果
void print(LL a, LL b)
{
//化简
LL t = gcd(a, b);
a /= t, b /= t;
//将分母上的负数移到分子上,并判断分子是否为负数
if (b < 0) a *= -1, b *= -1;
bool is_minus = a < 0;
//输出结果
if (is_minus) cout << "(";
if (b == 1) printf("%lld", a);
else
{
if (abs(a) >= b) printf("%lld ", a / b), a = abs(a) % b;
printf("%lld/%lld", a, b);
}
if (is_minus) cout << ")";
}
//加法运算
void add(LL a, LL b, LL c, LL d)
{
print(a, b), cout << " + ", print(c, d), cout << " = ";
a = a * d + c * b;
b = b * d;
print(a, b), cout << endl;
}
//减法运算
void sub(LL a, LL b, LL c, LL d)
{
print(a, b), cout << " - ", print(c, d), cout << " = ";
a = a * d - c * b;
b = b * d;
print(a, b), cout << endl;
}
//乘法运算
void mul(LL a, LL b, LL c, LL d)
{
print(a, b), cout << " * ", print(c, d), cout << " = ";
a = a * c;
b = b * d;
print(a, b), cout << endl;
}
//除法运算
void div(LL a, LL b, LL c, LL d)
{
print(a, b), cout << " / ", print(c, d), cout << " = ";
if (c == 0) puts("Inf");
else
{
a = a * d;
b = b * c;
print(a, b), cout << endl;
}
}
int main()
{
LL a, b, c, d;
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
add(a, b, c, d);
sub(a, b, c, d);
mul(a, b, c, d);
div(a, b, c, d);
return 0;
}