1.求最大公约数
2.分数相加
3.最后输出操作时要注意
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
ll GCD(ll a,ll b) {
while(b^=a^=b^=a%=b);
return a;
}
ll ansa, ansb;
int main() {
int n;
cin >> n;
string tmp;
cin >> tmp;
int pos = tmp.find('/');
ansa = stoi(tmp.substr(0, pos));
ansb = stoi(tmp.substr(pos+1));
ll g = GCD(ansa, ansb);
ansa /= g;
ansb /= g;
for(int i=1; i<n; i++) {
cin >> tmp;
int pos = tmp.find('/');
string a = tmp.substr(0, pos);
string b = tmp.substr(pos+1);
ll ta = stoi(a), tb = stoi(b);
g = GCD(ta, tb);
ta /= g;
tb /= g;
g = GCD(ansb, tb);
ansa = ansa*tb/g+ta*ansb/g;
ansb = ansb*tb/g;
g = GCD(ansa, ansb);
ansa /= g;
ansb /= g;
}
if(ansa*ansb < 0) cout << "-";
ansa = abs(ansa);
ansb = abs(ansb);
if(ansa == 0) cout << "0" << endl;
if(ansa >= ansb){
cout << ansa/ansb;
if(ansa%ansb){
cout << " ";
}else cout << endl;
}
if(ansa%ansb){
cout << ansa%ansb << "/" << ansb << endl;
}
return 0;
}