题目
分析
涉及到整型的运算,注意使用longlong类型的数据
要点
- 很多的小细节
知识点
- 找最小公约数
题解
#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;
}