Result: Accepted
Time: 5ms
Memory: 1712kB
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
#include<cctype>
using namespace std;
bool rn(int x) {
return (x % 400 == 0 || ((x % 4 == 0) && (x % 100 != 0)));
}
int gcd(int x, int y) {
if(x < 0)
x *= -1;
if(y < 0)
y *= -1;
if(x == 0)
return 1;
int r = x % y;
while(r) {
x = y;
y = r;
r = x % y;
}
return y;
}
int main() {
int t;
int a[105], b[105];
cin >> t;
int i, j;
char c;
for(i = 0; i < t; i++) {
cin >> a[i] >> c >> b[i];
}
for(i = 1; i < t; i++) {
int k = gcd(b[i - 1], b[i]);
a[i] = a[i - 1] * b[i] + a[i] * b[i - 1];
b[i] = b[i] * b[i - 1];
k = gcd(a[i], b[i]);
a[i] /= k;
b[i] /= k;
}
int zs = 0, fs;
if(a[t - 1] == 0)
cout << "0";
else if(abs(a[t - 1]) == b[t - 1]) {
cout << a[t - 1];
}
else if(abs(a[t - 1]) < b[t - 1]) {
cout << a[t - 1] << "/" << b[t - 1];
}
else {
if(a[t - 1] % b[t - 1] == 0)
cout << a[t - 1] / b[t - 1];
else
cout << a[t - 1] / b[t - 1] << " " << abs(a[t - 1]) - abs((a[t - 1] / b[t - 1]) * b[t - 1]) << "/" << b[t - 1];
}
return 0;
}