PAT Level B 1034 Four Arithmetic Operations of Rational Numbers (Problem Solving Ideas + AC Code)

topic:

This question requires writing a program to calculate the sum, difference, product, and quotient of two rational numbers.

Input format:

a1/b1 a2/b2The input gives two rational numbers in the form of fractions in one line, in which the numerator and denominator are all integers within the integer range, the negative sign can only appear before the numerator, and the denominator is not 0 .

Output format:

有理数1 运算符 有理数2 = 结果Output the sum, difference, product, and quotient of two rational numbers in the order of format in 4 lines respectively . Note that each rational number output must be the simplest form of the rational number k a/b, where kis the integer part and a/bthe simplest fraction part; if it is a negative number, parentheses must be added; if the division denominator is 0, then output Inf. The title ensures that there are no integers outside the integer range in the correct output.

Input sample 1:

2/3 -4/2

Output sample 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 sample 2:

5/3 0/6

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

Code length limit 16 KB
time limit 200 ms
memory limit 64 MB

 

problem solving ideas

  • We can first write a simplification function, which can reduce a rational number to the simplest.
  • For the addition, subtraction, multiplication and division of fractions, we can use the numerator and denominator as the unit to perform operations, and then call the simplification function to simplify the result.

 

AC code

#include iostream
using namespace std;

最大公因数
long long max(long long a, long long b)
{
    
    
    return b == 0amax(b, a%b);
}

化简函数
void print_huajian(long long fenzi, long long fenmu)
{
    
    
    long long ret_int, ret_fenzi, ret_fenmu;
    int flag1 = fenzi  010;
    int flag2 = fenmu  010;
    fenzi = labs(fenzi);
    fenmu = labs(fenmu);
    if (0 == fenmu)
    {
    
    
        cout  Inf;
        return;
    }
    if (0 == fenzi)
    {
    
    
        cout  0;
        return;
    }
    ret_int = fenzi  fenmu;
    fenzi %= fenmu;
    if (0 == fenzi)
    {
    
    
        if (flag1 + flag2 == 1)
        {
    
    
            cout  (-;
        }
        cout  ret_int;
        if (flag1 + flag2 == 1)
        {
    
    
            cout  );
        }
        return;
    }
    long long factor = max(fenzi, fenmu);
    fenzi = factor;
    fenmu = factor;
    ret_fenzi = fenzi;
    ret_fenmu = fenmu;
    if (flag1 + flag2 == 1)
    {
    
    
        cout  (-;
    }
    if (0 == ret_int)
        cout  ret_fenzi    ret_fenmu;
    else
        cout  ret_int     ret_fenzi    ret_fenmu;
    if (flag1 + flag2 == 1)
    {
    
    
        cout  );
    }
    return;
}

int main()
{
    
    
    long long fenzi1, fenmu1, fenzi2, fenmu2;
    scanf(%lld%lld %lld%lld, &fenzi1, &fenmu1, &fenzi2, &fenmu2);

    +
    print_huajian(fenzi1, fenmu1);
    cout   + ;
    print_huajian(fenzi2, fenmu2);
    cout   = ;
    print_huajian(fenzi1  fenmu2 + fenzi2  fenmu1, fenmu1  fenmu2);
    cout  endl;
    
    -
    print_huajian(fenzi1, fenmu1);
    cout   - ;
    print_huajian(fenzi2, fenmu2);
    cout   = ;
    print_huajian(fenzi1  fenmu2 - fenzi2  fenmu1, fenmu1  fenmu2);
    cout  endl;
    
    
    print_huajian(fenzi1, fenmu1);
    cout    ;
    print_huajian(fenzi2, fenmu2);
    cout   = ;
    print_huajian(fenzi1  fenzi2, fenmu1  fenmu2);
    cout  endl;
    
    
    print_huajian(fenzi1, fenmu1);
    cout    ;
    print_huajian(fenzi2, fenmu2);
    cout   = ;
    print_huajian(fenzi1  fenmu2, fenzi2  fenmu1);
    cout  endl;
    
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_70103775/article/details/130737330