计算分数 - 洛谷https://www.luogu.com.cn/problem/P1572
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <cstdlib>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 2000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
int gcd(int x, int y) {
if (y == 0) {
return x;
}
return gcd(y, x % y);
}
int main() {
int a, b, c, d;
scanf("%d/%d", &a, &b);
while (scanf("%d/%d", &c, &d) != EOF) {
int m = gcd(b, d);
a *= d / m;
c *= b / m;
a += c;
b *= d / m;
m = gcd(a, b);
a /= m;
b /= m;
if (b < 0) {
a = -a;
b = -b;
}
}
if (a == 0) {
printf("0");
} else if (b == 1) {
printf("%d", a);
} else {
printf("%d/%d", a, b);
}
}