You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
int a, B, c, d;
/**
测试用例:
2/1+1/3-1/4
答案25/12
-2/1+1/3-1/4
答案:-23/12
测试点6
7/4+1/3+8/5-3/2-6/9-6/4-5/2+6/2+4/9-3/9-2/3+5/2-5/4+3/9-3/8-5/8+6/8+3/8-4/7-5/7-3/6+6/9-5/6-5/7-5/2
*/
int gcd(int x, int y) {
if (y == 0) return x;
return gcd(y, x % y);
}
int lcm(int x, int y) {
return x * y / gcd(x, y);
}
int main() {
//和我一起念scanf大法好!
scanf("%d/%d", &a, &b);
//不断读取后面的数字,这玩意居然还能读入负号,牛!
while (scanf("%d/%d", &c, &d) != EOF) {
//通分,分母取最小公倍
int e = lcm(b, d);
//分子乘啊乘啊乘
int f = e / b * a + e / d * c;
//求最大公约数,约分
int g = gcd(e, f);
a = f / g;
b = e / g;
}
//因为求负数最大公约最小公倍也是可以的但可能最终会出现1259/-360这样的情况
//如果这时,需要特判一下,写成:-1259/360
if (b < 0) a = -a, b = -b;
//如果分母是1就输出分子
if (b == 1) printf("%d\n", a);
else printf("%d/%d\n", a, b);
return 0;
}