-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1081.cpp
83 lines (76 loc) · 1.67 KB
/
1081.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <set>
#include <stack>
#include <cstdio>
#include <cstring>
#include <climits>
#include <cstdlib>
using namespace std;
struct Rational {
long long num, den;
Rational(long long a, long long b):num(a), den(b) {
reduce();
}
static long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void reduce() {
long long a = num, b = den;
if (num < 0) a = -a;
long long g = gcd(a, b);
num /= g;
den /= g;
}
void operator+=(const Rational& rhs) {
num = num * rhs.den + den * rhs.num;
den *= rhs.den;
reduce();
}
void print() {
long long a = num, b = den;
if (a < 0) a = -a;
long long integer = a / b;
long long remain = a % b;
if (integer != 0) {
if (num < 0)
printf("-");
printf("%lld", integer);
if (remain != 0)
printf(" %lld/%lld", remain, b);
}
else {
if (num < 0)
printf("-");
if (remain != 0)
printf("%lld/%lld", remain, b);
else
printf("0");
}
}
};
int main() {
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
int n;
long long num, den;
scanf("%d", &n);
scanf("%lld/%lld", &num, &den);
auto res = Rational(num, den);
for (int i = 1; i < n; i++) {
scanf("%lld/%lld", &num, &den);
res += Rational(num, den);
}
res.print();
return 0;
}