21. A1088 Rational Arithmetic

1. 题目描述


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

2. 分析


  1. 分数的无损计算

  1. 通分

  1. 分子相加

  1. 化简分数为真分数

  1. 前面的系数= 分子/分母

  1. 分子 = 分子%分母

  1. 为 0 则单独进行处理

  1. 在除法时,会有小数产生,因此需要乘以公倍数 那不就傻了嘛,用变量去推导一下分数相除的公式就可以了

3 代码


运行超时!

原因在于求公约数函数上

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
long long gcd1(long long a,long long b)
{
    long long factor;
    factor = min(abs(a), abs(b));
    while(factor>1)
    {
        if(a%factor==0&&b%factor==0)
        {

            break;
        }
        factor--;
    }
    return factor;
}
long long gcd(long long t1, long long t2) {
    return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void Simplify(long long a, long long b ) {
    if(a*b==0)
    {
        if(a==0)
        {
            cout<<0;
        } else{
            printf("Inf");
        }
        return;
    }
    long long k;
    bool flag= false;
    if(a<0 && b>0) flag= true;//不能用a*b<0判断,会超出范围
    if(b<0 && a>0) flag= true;
    a = abs(a);
    b = abs(b);
    k = a/b;
    if(flag) printf("(-");
    if(k!=0) cout<<k;//输出k
    a = a%b;
    //化简 a/b
    if(a!=0)
    {
        long long factor = gcd(a,b);
        a = a/factor;
        b = b/factor;
        if(k!=0) printf(" %lld/%lld",a,b);
        else printf("%lld/%lld",a,b);


    }
    if(flag) printf(")");



}


int main() {
    long  long a, b, c, d;
    scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
//  不要出现小数
    Simplify(a,b);printf(" + ");Simplify(c,d);printf(" = ");Simplify(a*d+c*b,b*d);cout<<endl;
    Simplify(a,b);printf(" - ");Simplify(c,d);printf(" = ");Simplify(a*d-c*b,b*d);cout<<endl;
    Simplify(a,b);printf(" * ");Simplify(c,d);printf(" = ");Simplify(a*c,b*d);cout<<endl;
    Simplify(a,b);printf(" / ");Simplify(c,d);printf(" = ");Simplify(a*d,b*c);cout<<endl;

    return 0;
}  

3.1 更加简洁的思路

求公约数更快速的办法,不是暴力枚举

 long long gcd(long long t1, long long t2) {
    return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}

4. 收获


  1. 对于格式化输出,可以用printf,比cout要方便

  1. 求公约数的方法要注意

  1. 尽量先代入数学方法,再写代码,会简单很多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值