-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1022.cpp
More file actions
62 lines (51 loc) · 1.19 KB
/
1022.cpp
File metadata and controls
62 lines (51 loc) · 1.19 KB
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
/* URI Online Judge | 1022
TDA Racional
Recebendo 5% de WA '-'
*/
#include <iostream>
using namespace std;
int calculaMDC(int x, int y){
int resto;
resto = x % y;
while(resto){
x = y;
y = resto;
resto = x % y;
}
return y;
}
int main(void){
int n, N1, N2, D1, D2, num, den, mdc;
char op, ignore;
cin >> n;
while(n--){
cin >> N1 >> ignore >> D1 >> op >> N2 >> ignore >> D2;
switch(op){
case '+':
num = N1*D2 + N2*D1;
den = D1*D2;
break;
case '-':
num = N1*D2 - N2*D1;
den = D1*D2;
break;
case '*':
num = N1*D2;
den = D1*D2;
break;
case '/':
num = N1*D2;
den = N2*D1;
break;
default:
break;
}
cout << num << "/" << den << " = ";
mdc = calculaMDC(num, den);
if(mdc < 0)
cout << -(num / mdc) << "/" << -(den / mdc) << endl;
else
cout << num / mdc << "/" << den / mdc << endl;
}
return 0;
}