zoukankan      html  css  js  c++  java
  • A1088. Rational Arithmetic

    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

    #include<iostream>
    #include<cstdio>
    using namespace std;
    typedef struct NODE{
        long long up, dn;
        int inf;
        NODE(){
            inf = 0;
        }
    }node;
    long long gcd(long long a, long long b){
        if(b == 0)
            return a;
        else return gcd(b, a % b);
    }
    node add(node a, node b){
        node temp;
        temp.dn = a.dn * b.dn;
        temp.up = a.up * b.dn + b.up * a.dn;
        if(temp.dn < 0){
            temp.up *= -1;
            temp.dn *= -1;
        }
        long long fac = gcd(abs(temp.up), abs(temp.dn));
        if(fac != 0){
            temp.up = temp.up / fac;
            temp.dn = temp.dn / fac;
        }
        return temp;
    }
    node multp(node a, node b){
        node temp;
        temp.dn = a.dn * b.dn;
        temp.up = a.up * b.up;
        if(temp.dn < 0){
            temp.up *= -1;
            temp.dn *= -1;
        }
        long long fac = gcd(abs(temp.up), abs(temp.dn));
        if(fac != 0){
            temp.up = temp.up / fac;
            temp.dn = temp.dn / fac;
        }
        return temp;
    }
    node div(node a, node b){
        node temp;
        temp.dn = a.dn * b.dn;
        temp.up = a.up * b.up;
        if(temp.dn == 0){
            temp.inf = 1;
            return temp;
        }
        if(temp.dn < 0){
            temp.up *= -1;
            temp.dn *= -1;
        }
        long long fac = gcd(abs(temp.up), abs(temp.dn));
        if(fac != 0){
            temp.up = temp.up / fac;
            temp.dn = temp.dn / fac;
        }
        return temp;
    }
    void show(node a){
        if(a.inf == 1){
            printf("Inf");
        }else{
            if(a.up < 0){
                printf("(");
            }
            if(a.up == 0){
                printf("0");
                return;
            }
            if(abs(a.up) >= abs(a.dn)){
                if(abs(a.up) % abs(a.dn) == 0){
                    printf("%lld", a.up / a.dn);
                }else{
                    printf("%lld %lld/%lld", a.up / a.dn, abs(a.up) % abs(a.dn), a.dn);
                }
            }else{
                printf("%lld/%lld", a.up, a.dn);
            }
            if(a.up < 0)
                printf(")");
        }
    }
    int main(){
        long long temp1;
        node a, b, c, d;
        scanf("%lld/%lld %lld/%lld", &a.up, &a.dn, &b.up, &b.dn);
        long long fac = gcd(abs(a.up), abs(a.dn));
        if(fac != 0){
            a.up = a.up / fac;
            a.dn = a.dn / fac;
        }
        fac = gcd(abs(b.up), abs(b.dn));
        if(fac != 0){
            b.up = b.up / fac;
            b.dn = b.dn / fac;
        }
        c = b; c.up *= -1;
        d = b; swap(d.up, d.dn);
        node re1 = add(a, b);
        show(a); printf(" + "); show(b); printf(" = "); show(re1);
        printf("
    ");
        node re2 = add(a, c);
        show(a); printf(" - "); show(b); printf(" = "); show(re2);
        printf("
    ");
        node re3 = multp(a, b);
        show(a); printf(" * "); show(b); printf(" = "); show(re3);
        printf("
    ");
        node re4 = div(a, d);
        show(a); printf(" / "); show(b); printf(" = "); show(re4);
        cin >> temp1;
        return 0;
    }
    View Code

    总结:

    1、只有除法需要检测INF, 乘法不需要。

    2、本题需要输出 题目中输入的数字,所以当输入的分数不是最简分数时,需要先将输入化简,再计算、输出。

     
  • 相关阅读:
    life
    Android通过XML来定义Menu
    Android通过XML来定义Menu
    Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
    作为股权类投资人,我们的投资偏好和投资原则
    作为股权类投资人,我们的投资偏好和投资原则
    给TextView添加超链接的四种方式
    详解ExplosionField的使用,实现View的粉碎效果
    MySql中允许远程连接
    我的投资案例(3)-看好互联网和金融两大朝阳行业,参投入股垂直金融招聘平台"职业梦CareerDream.cn"
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/9473322.html
Copyright © 2011-2022 走看看