PAT甲级1088 Rational Arithmetic

1088
牛客网

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
结尾无空行

首先:如果找不到bug在哪,先上牛客网找同一题输入代码(因为牛客网出错时会给出测试点),牛客网上没有的话就看网上有没有人判断了各个测试点出错的原因或是有没有人给出供测试的自定义用例

(当然以上属于旁门左道,真正考试时没有这些办法,只能自己找bug,还是要锻炼自己找bug的能力😓)

一开始测试点3没过,原因在于我在convert函数中一开始写的条件是abs(a) > b,其实应该是大于等于

总结一下这类题:
这类题两个分数相加还要输出最简形式的题目,其实要考虑的点非常多,主要写写几个容易忘掉的点,其余的可以在代码里面看到:
1、因为题目中能出现负数的位置只有分子和整数,所以需要注意不要让分母带有负数(这一题主要会出现在除法),还要注意在计算最大公约数时两个数字不能带有负号

2、注意除法中出现除数为0的情况

3、因为题目中的数为long int,最好每一步都将得到的分数化为最简假分数形式,不要直到最后结果才化简,防止溢出

下面这个代码并不简洁优美,但我觉得还是挺好理解的😀

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
long long gcd(long long a,long long b){
    return b == 0?a:gcd(b,a % b);
}
long long convert(long long &a,long long &b){
    if(abs(a) >= b){
        long long k = 0;
        k = a / b;
        a %= b;
        return k;
    }
    else{
        return 0;
    }
}
void formatout(long long k,long long nominator,long long denominator){
    if(nominator == 0){
        if(k < 0){
            cout << '(' << k << ')';
        }
        else{
            cout << k;
        }
    }
    else if(k == 0){
        if(nominator < 0){
            cout << '(' << nominator << '/' << denominator << ')';
        }
        else{
            cout << nominator << '/' << denominator;
        }
    }
    else{
        if(k < 0){
            cout << '(' << k << ' ' << -nominator << '/' << denominator << ')';
        }
        else{
            cout << k << ' ' << nominator << '/' << denominator;
        }
    }
}
void myplus(long long kab,long long a,long long b,long long kcd,long long c,long long d){
    long long ori_a = a,ori_b = b,ori_c = c,ori_d = d;
    a += kab * b;
    c += kcd * d;
    long long temp = b * d / gcd(b,d);
    long long ac = a * d / gcd(b,d) + c * b / gcd(b,d);
    long long bd = temp;
    long long gd = gcd(abs(ac),bd);
    ac /= gd;
    bd /= gd;
    long long acbd = convert(ac,bd);
    formatout(kab,ori_a,ori_b);
    cout << " + ";
    formatout(kcd,ori_c,ori_d);
    cout << " = ";
    formatout(acbd,ac,bd);
    cout << endl;
    return;
}
void difference(long long kab,long long a,long long b,long long kcd,long long c,long long d){
    long long ori_a = a,ori_b = b,ori_c = c,ori_d = d;
    a += kab * b;
    c += kcd * d;
    long long temp = b * d / gcd(b,d);
    long long ac = a * d / gcd(b,d) - c * b / gcd(b,d);
    long long bd = temp;
    long long gd = gcd(abs(ac),bd);
    ac /= gd;
    bd /= gd;
    long long acbd = convert(ac,bd);
    formatout(kab,ori_a,ori_b);
    cout << " - ";
    formatout(kcd,ori_c,ori_d);
    cout << " = ";
    formatout(acbd,ac,bd);
    cout << endl;
    return;
}
void product(long long kab,long long a,long long b,long long kcd,long long c,long long d){
    long long ori_a = a,ori_b = b,ori_c = c,ori_d = d;
    a += kab * b;
    c += kcd * d;
    long long ac = a * c;
    long long bd = b * d;
    long long gd = gcd(abs(ac),bd);
    ac /= gd;
    bd /= gd;
    long long acbd = convert(ac,bd);
    formatout(kab,ori_a,ori_b);
    cout << " * ";
    formatout(kcd,ori_c,ori_d);
    cout << " = ";
    formatout(acbd,ac,bd);
    cout << endl;
    return;
}
void division(long long kab,long long a,long long b,long long kcd,long long c,long long d){
    long long ori_a = a,ori_b = b,ori_c = c,ori_d = d;
    a += kab * b;
    c += kcd * d;
    long long temp = b * d / gcd(b,d);
    if(c == 0||b == 0){
        formatout(kab,ori_a,ori_b);
        cout << " / ";
        formatout(kcd,ori_c,ori_d);
        cout << " = Inf";
        cout << endl;
        return;
    }
    long long ad = a * d;
    long long bc = b * c;
    if(bc < 0){
        ad = ad * -1;
        bc = bc * -1;
    }
    long long gd = gcd(abs(ad),bc);
    ad /= gd;
    bc /= gd;
    long long adbc = convert(ad,bc);
    formatout(kab,ori_a,ori_b);
    cout << " / ";
    formatout(kcd,ori_c,ori_d);
    cout << " = ";
    formatout(adbc,ad,bc);
    cout << endl;
    return;
}
int main(){
    long long a,b,c,d;
    scanf("%lld/%lld",&a,&b);
    scanf("%lld/%lld",&c,&d);
    long long maxAB = gcd(abs(a),b);
    long long maxCD = gcd(abs(c),d);
    a /= maxAB;
    b /= maxAB;
    c /= maxCD;
    d /= maxCD;
    long long kab = convert(a,b);
    long long kcd = convert(c,d);
    myplus(kab,a,b,kcd,c,d);
    difference(kab,a,b,kcd,c,d);
    product(kab,a,b,kcd,c,d);
    division(kab,a,b,kcd,c,d);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值