【PAT甲级 - C++题解】1088 Rational Arithmetic

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1088 Rational Arithmetic (pintia.cn)
🔑中文翻译:有理数运算
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1088 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
题意

给定两个有理数,你的任务是实现基本算术,即计算它们的和,差,积和商。

输入共一行,以 a1/b1 a2/b2 的形式给出两个有理数。

分子和分母都在 long int 范围内,如果存在负号,则只能出现在分子前面,分母保证为非零数字。

思路

这道题就是模拟分数的加减乘除运算即可,假设有两个分数 a/bc/d ,则运算公式分别如下:

【加法】 a = a ∗ d + c ∗ b a = a * d + c * b a=ad+cb b = b ∗ d b = b * d b=bd

【减法】 a = a ∗ d − c ∗ b a = a * d - c * b a=adcb b = b ∗ d b = b * d b=bd

【乘法】 a = a ∗ c a = a * c a=ac b = b ∗ d b = b * d b=bd

【除法】 a = a ∗ d a = a * d a=ad b = b ∗ c b = b * c b=bc(另外,如果 c0 ,则直接输出 Inf

输出分数的时候需要先对分子分母进行化简,求它们的最大公约数。同时,要注意负数情况,如果分母为负数,则需要转移到分子上再输出,且输出负数时要加括号。

代码
#include<bits/stdc++.h>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值