-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpta5
52 lines (47 loc) · 905 Bytes
/
pta5
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
#include <stdio.h>
int gcd(int m,int n)
{
while (n) {
int t = n;
m %= n;
n = m;
m = t;
}
return m;
}
int main() {
int N;
scanf("%d", &N);
int i,a[N],b[N];
int x,y,c,d,m,n;
for (i = 0; i < N; i++) {
scanf("%d/%d", &a[i], &b[i]);
}
x = a[0];
y = b[0];
for (i = 1; i < N; i++) {
x = x * b[i] + a[i] * y;
y *= b[i];
c = gcd(x, y);
if(c!=0){
x /= c;
y /= c;
}
}
if(x == 0 && y != 0){
printf("0");
}else{
d = x/y;
x %= y;
if (d != 0 && x != 0) {
printf("%d %d/%d", d, x, y);
}
else if (d != 0 && x == 0) {
printf("%d", d);
}
else {
printf("%d/%d",x, y);
}
}
return 0;
}