当前位置: 代码迷 >> 综合 >> PAT A1088. Rational Arithmetic (20)
  详细解决方案

PAT A1088. Rational Arithmetic (20)

热度:6   发布时间:2023-12-24 07:41:52.0

1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
思路分析:

分数四则运算,除法前记得特判0的情况。

题解:

#include <cstdio>
#include <algorithm>
using namespace std;struct Fraction{long long up, down;
}f1,f2;long long gcd(long long a, long long b){if(b == 0) return a;else return gcd(b, a % b);
}Fraction reduction(Fraction c){if(c.down < 0){c.up = -c.up;c.down = -c.down;}if(c.up == 0) c.down = 1;else{long long d = gcd(abs(c.up), c.down);c.up /= d;c.down /= d;}return c;
}Fraction add(Fraction a, Fraction b){Fraction c;c.up = a.up * b.down + a.down * b.up;c.down = a.down * b.down;return reduction(c);
}Fraction sub(Fraction a, Fraction b){Fraction c;c.up = a.up * b.down - a.down * b.up;c.down = a.down * b.down;return reduction(c);
}Fraction mul(Fraction a, Fraction b){Fraction c;c.up = a.up * b.up;c.down = a.down * b.down;return reduction(c);
}Fraction div(Fraction a, Fraction b){Fraction c;c.up = a.up * b.down;c.down = a.down * b.up;return reduction(c);
}void print(Fraction a){a = reduction(a);if(a.up < 0) printf("(");if(a.down == 1) printf("%lld", a.up);else if(abs(a.up) > a.down) printf("%lld %lld/%lld", a.up/a.down, abs(a.up)%a.down, a.down);else printf("%lld/%lld", a.up, a.down);if(a.up < 0) printf(")");
}int main(){scanf("%lld/%lld %lld/%lld", &f1.up, &f1.down, &f2.up, &f2.down);f1 = reduction(f1), f2 = reduction(f2);print(f1);printf(" + ");print(f2);printf(" = ");print(add(f1,f2));printf("\n");print(f1);printf(" - ");print(f2);printf(" = ");print(sub(f1,f2));printf("\n");print(f1);printf(" * ");print(f2);printf(" = ");print(mul(f1,f2));printf("\n");print(f1);printf(" / ");print(f2);printf(" = ");if(f2.up == 0) printf("Inf");else print(div(f1,f2));printf("\n");return 0;
}





  相关解决方案