PAT grade B exercise 1034 four operations of rational numbers

Keywords: C C++ Algorithm

;

Title:

Enter two fractional rational numbers in the form of A1 / B1 and A2 / B2 in one line, where the numerator and denominator are all integers within the integer range, the negative sign can only appear in front of the numerator, and the denominator is not 0.

Output format:

Output the sum, difference, product and quotient of two rational numbers in the format order of rational number 1 operator rational number 2 = result in 4 rows respectively. Note that each rational number output must be the simplest form k a/b of the rational number, where k is the integer part and a/b is the simplest fraction part; If it is a negative number, parentheses shall be added; If the division denominator is 0, Inf is output. The problem is to ensure that there are no integers exceeding the integer range in the correct output.

Input example 1:

2/3 -4/2

Output example 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

Input example 2:

5/3 0/6

Output example 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

Idea:

There are a lot of things to judge in this problem. I don't have ideas. I have ideas only after reading the analysis. First, I don't know how to judge whether the final operation result is Inf. at first, I want to simplify the two fractions and then simplify the analog operation, but I don't know how to operate with fractions. The second is that we don't know how to reduce the score with the maximum common divisor. Finally, the operation process of not knowing how to express scores in code.

The correct idea should be to calculate the numerator and denominator, and then simplify it.

  • First, judge whether the numerator denominator is 0, and output 0 and Inf accordingly
  • Secondly, judge whether the numerator and denominator have the same number, and output the "-" sign accordingly
  • Again, judge whether the numerator and denominator can be divided, and output an integer or a fraction
  • Finally, the maximum common divisor is used to simplify the numerator denominator

Note:

  • The multiplication of fractions involves the multiplication of two 109, and the long long type should be used
  • To judge whether the numerator and denominator are different, do not write to judge whether m*n is less than 0, because the result of m*n may exceed the length of long long int, resulting in overflow greater than 0

code:

#include <bits/stdc++.h>
using namespace std;

long long gcd(long long a, long long b);
void simplify_fraction(long long a, long long b);

int main(){
    long long a1, b1, a2, b2;
    char m1, m2;
    
    cin >> a1 >> m1 >> b1 >> a2 >> m2 >> b2;
    
    simplify_fraction(a1, b1); printf(" + "); simplify_fraction(a2, b2); printf(" = "); simplify_fraction(a1*b2+a2*b1, b1*b2); printf("\n");
    simplify_fraction(a1, b1); printf(" - "); simplify_fraction(a2, b2); printf(" = "); simplify_fraction(a1*b2-a2*b1, b1*b2); printf("\n");
    simplify_fraction(a1, b1); printf(" * "); simplify_fraction(a2, b2); printf(" = "); simplify_fraction(a1*a2, b1*b2); printf("\n");
    simplify_fraction(a1, b1); printf(" / "); simplify_fraction(a2, b2); printf(" = "); simplify_fraction(a1*b2, b1*a2); printf("\n");
}

//The maximum common divisor is obtained by recursive rolling division method, which is used to simplify the numerator denominator
long long gcd(long long a, long long b){
    //If b is 0, a is the greatest common divisor
    return b == 0 ? a : gcd(b, a%b);
}

//Reduced fraction
void simplify_fraction(long long a, long long b){
    if(b == 0){
        cout << "Inf";
    }
    else if(a == 0){
        cout << 0;
    }
    else{
        bool flag = (a < 0 && b > 0) || (a > 0 && b < 0);
        a = abs(a);
        b = abs(b);
        //Take the absolute value and then output the calculation quotient
        long long x = a/b;
        if(x == 0){
            //If the quotient is 0, it indicates that it is a true fraction, and there is no need to output a positive integer
            if(flag){
                printf("(-");
                printf("%lld/%lld", a/gcd(a,b), b/gcd(a,b));
                printf(")");
            }
            else{
                printf("%lld/%lld", a/gcd(a,b), b/gcd(a,b));
            }
        }
        else{
            //If the quotient is not 0, judge whether to divide. If the division directly outputs a positive integer, if it is not the division output with fractional format, pay attention to updating the value of the numerator
            a = a%b;
            if(flag){
                printf("(-");
                printf("%lld", x);
                if(a != 0)//Determine whether to divide
                    printf(" %lld/%lld", a/gcd(a,b), b/gcd(a,b));
                printf(")");
            }
            else{
                printf("%lld", x);
                if(a != 0)
                    printf(" %lld/%lld", a/gcd(a,b), b/gcd(a,b));
            }
        }
    }
}

Learned and recalled:

  • Finding common divisor by rolling Division

    Divide the larger number by the smaller number, remove the divisor with the remainder (the first remainder), and remove the first remainder with the remainder (the second remainder). Repeat until the last remainder is 0.
    If it is to find the greatest common divisor of two numbers, the last divisor is the greatest common divisor of the two numbers.

Reference resolution:

1034. Four operations of rational numbers (20)-PAT class B true problem

Posted by stopblackholes on Tue, 07 Sep 2021 13:36:26 -0700