PTA(Basic Level)1034.有理数四则运算

本题要求编写程序,计算 2 个有理数的和、差、积、商。

输入格式:

输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。

输出格式:

分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。

输入样例 1:
2/3 -4/2
输出样例 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
输入样例 2:
5/3 0/6
输出样例 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
思路

简单来说就是分数的加减乘除的问题

  • 分数化简的思路——求分母和分子的最大公约数,同时÷最大公约数
  • 分数相加的思路——求两个分母的最大公倍数,分子乘以对应的因子,相加即可
  • 设\(a,b\)的最大公约数为\(d\),有性质:\(最大公倍数a*b/d = 最大公倍数\)
  • 乘除法注意判断是否为0即可
  • 为了避免溢出问题,我这里统一采用了long long

有更好的写法是用结构体写,这样代码看起来会更好看

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b)
{
    if(b == 0)  return a;
    else return gcd(b, a%b);
}   //求最大公约数

void reduction(ll &a1, ll &b1)
{
    if(a1 == 0)
        return;
    ll t = gcd(a1, b1);
    a1 /= t;
    b1 /= t;
}   //分子分母约分

void standard_print(ll up, ll down)
{
    if(up * down < 0)      //保证负数的时候负数出现在分子
    {
        up = -abs(up);
        down = abs(down);
    }
    if(up == 0)
    {
        printf("0");
        return;
    }
    if(abs(up) >= abs(down))
    {
        ll k = up / down;
        up -= k * down;
        up = abs(up);
        if(k < 0)
            if(up == 0)
                printf("(-%lld)", abs(k));
            else printf("(-%lld %lld/%lld)", abs(k), up, down);
        else
            if(up == 0)
                printf("%lld", k);
            else printf("%lld %lld/%lld", k, up, down);
    }else
    {
        if(up < 0)
            printf("(%lld/%lld)", up, down);
        else
            printf("%lld/%lld", up, down);
    }
}   //控制标准输出形式,按照题目要求

void do_add(ll a1, ll b1, ll a2, ll b2)
{
    ll gcd_value = gcd(b1, b2);
    ll lcm_value = b1 / gcd_value * b2;
    ll up = a1 * (lcm_value / b1) + a2 * (lcm_value / b2);
    ll down = lcm_value;
    reduction(up, down);
    standard_print(a1, b1);
    printf(" + ");
    standard_print(a2, b2);
    printf(" = ");
    standard_print(up, down);
    printf("\n");
}

void do_sub(ll a1, ll b1, ll a2, ll b2)
{
    ll gcd_value = gcd(b1, b2);
    ll lcm_value = b1 / gcd_value * b2;
    ll up = a1 * (lcm_value / b1) - a2 * (lcm_value / b2);      //加法和减法唯一的区别就在这
    ll down = lcm_value;
    reduction(up, down);
    standard_print(a1, b1);
    printf(" - ");
    standard_print(a2, b2);
    printf(" = ");
    standard_print(up, down);
    printf("\n");
}   

void do_mult(ll a1, ll b1, ll a2, ll b2)
{
    ll up, down;
    if(a1 == 0 || a2 == 0)      //任意一乘数为0则整体为0,不用再计算了
    {
        up = 0;
    }else
    {
        up = a1 * a2;
        down = b1 * b2;
        reduction(up, down);
    }
    standard_print(a1, b1);
    printf(" * ");
    standard_print(a2, b2);
    printf(" = ");
    standard_print(up, down);
    printf("\n");
}

void do_ext(ll a1, ll b1, ll a2, ll b2)
{
    ll up, down;
    bool illegal = false;   //判断除法是否合法
    if(a2 == 0)
    {
        illegal = true;
    }else
    {
        up = a1 * b2;
        down = b1 * a2;
        reduction(up, down);
    }
    standard_print(a1, b1);
    printf(" / ");
    standard_print(a2, b2);
    printf(" = ");
    if(illegal)
        printf("Inf");
    else standard_print(up, down);
}

int main()
{
    ll a1,b1,a2,b2;
    scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
    reduction(a1, b1); reduction(a2, b2);
//  cin >> a1 >> b1;
//    standard_print(a1, b1);
    do_add(a1, b1, a2, b2);
    do_sub(a1, b1, a2, b2);
    do_mult(a1, b1, a2, b2);
    do_ext(a1, b1, a2, b2);
    return 0;
}
引用

https://pintia.cn/problem-sets/994805260223102976/problems/994805287624491008

上一篇:贝叶斯公式


下一篇:概率论基础_事件、条件概率、全概率公式、贝叶斯公式