1034 有理数四则运算 (20 分)
输入样例 1:
2/3 -4/2
输出样例 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
输入样例 2:
5/3 0/6
输出样例 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
????,看柳神
#include <iostream>
#include <cmath>
using namespace std;
long long a, b, c, d;
long long gcd(long long t1, long long t2) {
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void func(long long m, long long n) {
if (m * n == 0) {
printf("%s", n == 0 ? "Inf" : "0");
return ;
}
bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));
m = abs(m); n = abs(n);
long long x = m / n;
printf("%s", flag ? "(-" : "");
if (x != 0) printf("%lld", x);
if (m % n == 0) {
if(flag) printf(")");
return ;
}
if (x != 0) printf(" ");
m = m - x * n;
long long t = gcd(m, n);
m = m / t; n = n / t;
printf("%lld/%lld%s", m, n, flag ? ")" : "");
}
int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
return 0;
}