1088 Rational Arithmetic (PAT甲级)

这道题折磨了我很久,最后发现bug竟然是因为使用了abs()函数在long long类型上……必须使用llabs(). 或者像其他人一样先using namespace std; 就可以直接用abs()。

https://bbs.csdn.net/topics/392139827?list=lz

这里有相关的讨论。“C++标准中,cstdio声明的是

namespace std {
          int abs (          int n);
     long int abs (     long int n);
long long int abs (long long int n);
}

C语言标准的stdio.h中声明的只是int abs (int n)。 所以,如果不“包含cstdio并指定在std名空间中查找”的话,编译器只会通过stdio.h找到来自C语言的int ::abs (int n),而不是你想要的int long long int std::abs (long long int n)。 如果不使用using namespace std,而明确以std::abs(参数)的形式调用,编译器就不会在你只包含stdio.h的时候悄悄使用int ::abs (int n)。(这也是经典C++编程中不建议使用using namespace std的理由。)”

#include 
#include 
#include 

long long a1, b1, a2, b2, numerator, denominator;

long long gcd(long long a, long long b){
    return b == 0 ? a : gcd(b, a % b);
}

void print1(long long a, long long b){
    if(b == 0){
        printf("Inf");
        return;
    }
    if(a == 0){
        printf("0");
        return;
    }
    bool flag = true;
    if((a < 0 && b > 0) || (a > 0 && b < 0)){
        printf("(-");
        flag = false;
    }
    a = llabs(a);
    b = llabs(b);
    long long tmp = gcd(a, b);
    a /= tmp;
    b /= tmp;
    long k = a / b;
    a %= b;
    if(k != 0){
        printf("%ld%s", k, a == 0 ? "" : " ");
    }
    if(a != 0){
        printf("%lld/%lld", a, b);
    }
    if(!flag){
        printf(")");
    }
}

void sum(){
    numerator = a1 * b2 + a2 * b1;
    denominator = b1 * b2;
    print1(a1, b1);
    printf(" + ");
    print1(a2, b2);
    printf(" = ");
    print1(numerator, denominator);
    printf("\n");
}

void difference(){
    numerator = a1 * b2 - a2 * b1;
    denominator = b1 * b2;
    print1(a1, b1);
    printf(" - ");
    print1(a2, b2);
    printf(" = ");
    print1(numerator, denominator);
    printf("\n");
}

void product(){
    numerator = a1 * a2;
    denominator = b1 * b2;
    print1(a1, b1);
    printf(" * ");
    print1(a2, b2);
    printf(" = ");
    print1(numerator, denominator);
    printf("\n");
}

void quotient(){
    numerator = a1 * b2;
    denominator = b1 * a2;
    print1(a1, b1);
    printf(" / ");
    print1(a2, b2);
    printf(" = ");
    print1(numerator, denominator);
    printf("\n");
}

int main(){
    scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
    sum();
    difference();
    product();
    quotient();
    return 0;
}

题目如下:

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

你可能感兴趣的:(PAT甲级,c++,pat考试)