zoukankan      html  css  js  c++  java
  • PAT甲级——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

    就一句话,细节很重要!
     1 #include <iostream>
     2 using namespace std;
     3 long long dv, res1, res2;
     4 long long DIV(long long a, long long b)
     5 {
     6     if (b == 0)
     7         return abs(a);
     8     return DIV(b, a%b);
     9 }
    10 void print(long long a1, long long b1, long long a2, long long b2, char c)
    11 {
    12     if (a1 == 0)
    13         printf("%d %c ", 0, c);
    14     else
    15     {
    16         printf("%s", a1 > 0 ? "" : "(");
    17         dv = DIV(a1, b1);
    18         a1 /= dv;
    19         b1 /= dv;
    20 
    21         if (a1 / b1 != 0)
    22             printf("%d", a1 / b1);
    23         if (a1 - b1 * (a1 / b1) != 0)
    24             printf("%s%d/%d", a1 / b1 != 0 ? " " : "", a1 / b1 != 0 ? abs(a1 - b1 * (a1 / b1)) : a1, b1);
    25         printf("%s %c ", a1 > 0 ? "" : ")", c);
    26     }
    27 
    28     if(a2 == 0)
    29         printf("%d %s ", 0, "=");
    30     else
    31     {
    32         printf("%s", a2 > 0 ? "" : "(");
    33         dv = DIV(a2, b2);
    34         a2 /= dv;
    35         b2 /= dv;
    36         if (a2 / b2 != 0)
    37             printf("%d", a2 / b2);
    38         if (a2 - b2 * (a2 / b2) != 0)
    39             printf("%s%d/%d", a2 / b2 != 0 ? " " : "", a2 / b2 != 0 ? abs(a2 - b2 * (a2 / b2)) : a2, b2);
    40         printf("%s %s ", a2 > 0 ? "" : ")", "=");
    41     }
    42 
    43     if (res1 == 0)
    44     {
    45         printf("%d
    ",0);
    46         return;
    47     }
    48     else if (res2 == 0)
    49     {
    50         printf("Inf
    ");
    51         return;
    52     }
    53     printf("%s", res1 > 0 ? "" : "(");
    54     dv = DIV(res1, res2);
    55     res1 /= dv;
    56     res2 /= dv;
    57     if (res1 / res2 != 0)
    58         printf("%d", res1 / res2);
    59     if (res1 - res2 * (res1 / res2) != 0)
    60         printf("%s%d/%d", res1 / res2 != 0 ? " " : "", res1 / res2 != 0 ? abs(res1 - res2 * (res1 / res2)) : res1, res2);
    61     printf("%s
    ", res1 > 0 ? "" : ")");
    62 }
    63 int main()
    64 {
    65     char c;
    66     long long a1, b1, a2, b2;
    67     cin >> a1 >> c >> b1 >> a2 >> c >> b2;
    68     // +
    69     res1 = a1 * b2 + a2 * b1;
    70     res2 = b1 * b2;
    71     print(a1, b1, a2, b2, '+');
    72     // -
    73     res1 = a1 * b2 - a2 * b1;
    74     res2 = b1 * b2;
    75     print(a1, b1, a2, b2, '-');
    76     // *
    77     res1 = a1 * a2;
    78     res2 = b1 * b2;
    79     print(a1, b1, a2, b2, '*');
    80     // /
    81     res1 = a2 > 0 ? a1 * b2 : a1 * b2*-1;
    82     res2 = b1 * abs(a2);
    83     print(a1, b1, a2, b2, '/');
    84     return 0;
    85 }
  • 相关阅读:
    七个高效的文本编辑习惯(以Vim为例)
    rbx1 package 下载安装过程
    ros机器人开发概述
    ROS BY EXAMPLE 1 -- 环境设置与安装
    除法取模练习(51nod 1119 & 1013 )
    kinect driver install (ubuntu 14.04 & ros-indigo)
    ros问题总结
    200行代码搞定炸金花游戏(PHP版)
    JavaScript方法call,apply,caller,callee,bind的使用详解及区别
    javascript中apply、call和bind的区别
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11342960.html
Copyright © 2011-2022 走看看