-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1081.cpp
76 lines (67 loc) · 1.64 KB
/
A1081.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
#include <cstdio>
#include <cstdlib>
struct Fraction{
long long up, down;
Fraction() : up(0), down(1) {}
} temp, sum;
long long gcd(long long a, long long b){
if(b == 0) return a;
else return gcd(b, a % b);
}
Fraction &reduction(Fraction &result){
if(result.down < 0){
result.up = 0 - result.up;
result.down = 0 - result.down;
}
if(result.up == 0){
result.down = 1;
}else{
long long d = gcd(abs(result.up), abs(result.down));
result.up /= d;
result.down /= d;
}
return result;
}
Fraction add(const Fraction &f1, const Fraction &f2){
Fraction result;
result.up = f1.up*f2.down + f2.up*f1.down;
result.down = f1.down * f2.down;
return reduction(result);
}
Fraction minu(const Fraction &f1, const Fraction &f2){
Fraction result;
result.up = f1.up*f2.down - f2.up*f1.down;
result.down = f1.down * f2.down;
return reduction(result);
}
Fraction multi(const Fraction &f1, const Fraction &f2){
Fraction result;
result.up = f1.up * f2.down;
result.down = f1.down * f2.down;
return reduction(result);
}
Fraction divide(const Fraction &f1, const Fraction &f2){
Fraction result;
result.up = f1.up * f2.down;
result.down = f1.down * f2.up;
return reduction(result);
}
void showResult(Fraction &result){
result = reduction(result);
if(result.down == 1) printf("%lld\n", result.up);
else if(abs(result.up) > result.down){
printf("%lld %lld/%lld\n", result.up/result.down, abs(result.up)%result.down, result.down);
}else{
printf("%lld/%lld\n", result.up, result.down);
}
}
int main(){
int N;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%lld/%lld", &temp.up, &temp.down);
sum = add(sum, temp);
}
showResult(sum);
return 0;
}