其他分享
首页 > 其他分享> > 【刷题】牛客网——Rational Arithmetic (20)

【刷题】牛客网——Rational Arithmetic (20)

作者:互联网

牛客网——Rational Arithmetic
(20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

输入描述
每个输入文件包含一个测试用例,它在一行中给出了格式为“a1/b1 a2/b2”的两个有理数。
分子和分母都在long int的范围内。如果有负号,它必须只出现在分子前面。分母保证是非零数。

输出描述
对于每个测试用例,分别在4行中打印两个有理数的和、差、积和商。
每一行的格式是“number1操作符number2 = result”。注意所有有理数都必须是最简形式“k a/b”,其中k是整数部分,而a/b是最简分数部分。
如果数字是负数,它必须包含在一对括号中。
如果分母中除法为零,输出“Inf”作为结果。它保证所有输出整数都在long int的范围内。

#include <iostream>
#include <cmath>
using namespace std;

// 结构体表示分数
struct fraction
{
    long long up;
    long long down;
};

// 求最大公约数
long long gcd(long long a, long long b)
{
    if(b == 0) 
        return a;
    else 
        return gcd(b, a % b);
}

// 将结果化简为题目要求的形式
fraction reduction(fraction result)
{
    // 分母为负,将符号转移给分子
    if(result.down < 0)
    {
        result.up = -result.up;
        result.down = -result.down;
    }
    if(result.up == 0) 
        result.down = 1;
    else
    {
        // 约分
        long long d = gcd(abs(result.up), abs(result.down));
        result.up /= d;
        result.down /= d;
    }
    return result;
}

// 分数加法运算
fraction add(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.down + b.up * a.down;
    result.down = a.down * b.down;
    return result;
}

// 分数减法运算
fraction minu(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.down - b.up * a.down;
    result.down = a.down * b.down;
    return result;
}

// 分数乘法运算
fraction multi(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.up;
    result.down = a.down * b.down;
    return result;
}

// 分数除法运算
fraction divide(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.down;
    result.down = a.down * b.up;
    return result;
}

// 打印数字
void printresult(fraction a)
{
    a = reduction(a);
    // 负号需要带括号
    if(a.up < 0)
        cout << "(";
    // 整数
    if(a.down == 1)
        cout << a.up;
    // 假分数
    else if(abs(a.up) > a.down)
        cout << a.up / a.down << " " << abs(a.up) % a.down << "/" << a.down;
    // 真分数
    else
        cout << a.up << "/" << a.down;
    if(a.up < 0)
        cout << ")";
}

int main()
{
    fraction a, b;
    scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
    printresult(a); cout << " + "; printresult(b); cout << " = "; printresult(add(a, b)); cout << endl;
    printresult(a); cout << " - "; printresult(b); cout << " = "; printresult(minu(a, b)); cout << endl;
    printresult(a); cout << " * "; printresult(b); cout << " = "; printresult(multi(a, b)); cout << endl;
    printresult(a); cout << " / "; printresult(b); cout << " = ";
    if(b.up == 0) 
        cout << "Inf" << '\n';
    else
    {
        printresult(divide(a,b));
        cout << endl;
    }
    return 0;
}

标签:20,up,long,down,牛客,result,fraction,return,Arithmetic
来源: https://blog.csdn.net/m0_46613023/article/details/117593028