zoukankan      html  css  js  c++  java
  • PAT Advanced 1088 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.

    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个坑点。

    第一个是,输入后进行约分,因为不约分,会通分溢出。

    第二个坑点就是,gcd要加绝对值,否则会有问题。

    #include <iostream>
    using namespace std;
    long long a, b, c, d;
    /** 最大公约数 */
    long long gcd(long long n1, long long n2) {
        return n2 == 0 ? n1 : gcd(n2, n1 % n2);
    }
    /** 约分 */
    void reduce(long long& n1, long long& n2) {
        long long num_gcd = gcd(abs(n1), abs(n2));
        if(num_gcd != 0) {
            n1 /= num_gcd;
            n2 /= num_gcd;
        }
    }
    /** 打印 */
    void print(long long n1, long long n2) {
        reduce(n1, n2);
        if(n2 == 0) printf("Inf");
        else if(n1 == 0) printf("0");
        else {
            bool rightP = false;
            if(n1 * n2 < 0) {
                printf("(-");
                rightP = true;
            }
            n1 = abs(n1); n2 = abs(n2);
            if(n1 / n2 != 0) printf("%lld", n1/n2);
            if(n1 % n2 != 0 && n1 / n2 != 0) printf(" ");
            if(n1 % n2 != 0) printf("%lld/%lld", n1%n2, n2);
            if(rightP) printf(")");
        }
    }
    int main() {
        scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
        reduce(a, b); reduce(c, d);
        /** 通分 */
        long long above1 = a * d, above2 = c * b, under = b * d;
        /** 计算 */
        long add = above1 + above2, un_add = under;
        long sub = above1 - above2, un_sub = under;
        long mul = above1 * above2, un_mul = under * under;
        long div = above1, un_div = above2;
        /** 打印 */
        print(a, b); printf(" + "); print(c, d); printf(" = "); print(add, un_add); printf("
    ");
        print(a, b); printf(" - "); print(c, d); printf(" = "); print(sub, un_sub); printf("
    ");
        print(a, b); printf(" * "); print(c, d); printf(" = "); print(mul, un_mul); printf("
    ");
        print(a, b); printf(" / "); print(c, d); printf(" = "); print(div, un_div); printf("
    ");
        system("pause");
        return 0;
    }
  • 相关阅读:
    bridge桥接模式
    docker部署mysql实现远程访问
    翻下旧资料,发现96年考过foxbase二级
    这几年专注于流程管理与RPA落地
    201521123080《Java程序设计》第10周学习总结
    201521123034《Java程序设计》第十周学习总结
    Beta版本冲刺计划及安排
    团队作业7——Alpha冲刺之事后诸葛亮
    团队作业6——展示博客(Alpha版本)
    团队作业4——第一次项目冲刺(Alpha版本)第一天+第二天+第三天+第四天+第五天+第六天+第七天
  • 原文地址:https://www.cnblogs.com/littlepage/p/12264452.html
Copyright © 2011-2022 走看看