PAT甲级 1088 Rational Arithmetic (20 分)

该博客主要讨论如何实现有理数的加减乘除运算。通过输入两个有理数的分子和分母,程序会计算它们的和、差、积和商,并确保结果是最简形式。如果除数为零,则输出'Inf'。示例中给出了具体的操作过程和输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <cstdio>
#include <cmath>

typedef long long LL;
using namespace std;

LL gcd(LL a, LL b) {
    return !b ? a : gcd(b, a % b);
}

LL abs(LL a) {
    if (a < 0)
        return -a;
    else
        return a;
}

void print(LL a, LL b) {
    bool flag = false;
    if (a == 0) {
        printf("0");
        return;
    }

    if (a < 0) {
        printf("(");
        if (a / b) {
            printf("%lld", a / b);
            flag = true;
        }
        if (a % b != 0) {
            if (flag)
                printf(" %lld/%lld)", abs(a % b), b);
            else
                printf("%lld/%lld)", a % b, b);
        } else
            printf(")");
    } else {
        if (a / b) {
            printf("%lld", a / b);
            flag = true;
        }
        if (a % b != 0) {
            if (flag)
                printf(" %lld/%lld", a % b, b);
            else
                printf("%lld/%lld", a % b, b);
        }
    }
}


int main() {
    LL a1, a2, b1, b2;//a1是第一个分子,a2是分母
    scanf("%lld/%lld %lld/%lld", &a1, &a2, &b1, &b2);
    LL a3 = gcd(abs(a1), a2);
    LL b3 = gcd(abs(b1), b2);
    a1 /= a3;
    a2 /= a3;
    b1 /= b3;
    b2 /= b3;
    for (int i = 0; i < 4; ++i) {
        print(a1, a2);
        if (i == 0)
            printf(" + ");
        else if (i == 1)
            printf(" - ");
        else if (i == 2)
            printf(" * ");
        else
            printf(" / ");
        print(b1, b2);
        printf(" = ");
        LL c1, c2;
        LL k = gcd(a2, b2);
        c2 = a2 / k * b2;
        if (i == 0)
            c1 = c2 / a2 * a1 + c2 / b2 * b1;
        else if (i == 1)
            c1 = c2 / a2 * a1 - c2 / b2 * b1;
        else if (i == 2) {
            c1 = a1 * b1;
            c2 = b2 * a2;
        } else if (i == 3) {
            c1 = a1 * b2;
            c2 = b1 * a2;
            if (c2 < 0) {
                c2 = -c2;
                c1 = -c1;
            }
            if (!c2) {
                printf("Inf\n");
                break;
            }
        }
        LL m = gcd(abs(c1), abs(c2));
        c1 /= m;
        c2 /= m;
        print(c1, c2);
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XdpCs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值