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;
    }
  • 相关阅读:
    ios启动画面
    不让精灵移除屏幕外 重写setPosition方法
    post和get请求方式的区别
    IOS开发之手势——UIGestureRecognizer 共存
    Xcode 中的GDB的命令
    [UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别
    应用图标 ICON
    cocos2d 1.01不能运行以前版本工程的问题
    SQL 中数据类型的转换 转
    SQL Server中sql语句执行时间
  • 原文地址:https://www.cnblogs.com/littlepage/p/12264452.html
Copyright © 2011-2022 走看看