-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050.cpp
96 lines (83 loc) · 1.51 KB
/
050.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
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cmath>
using namespace std;
int fasteuc(int a, int b) {
int l, j, rem;
if (a == b) { return a; }
if (a > b) { l = a; j = b; }
else { l = b; j = a; }
if (j == 0) { return l; }
if (l%j == 0) { return j; }
if (!(l % 2) && j % 2 == 0) {
l = l / 2; j = j / 2;
return fasteuc(l, j) * 2;
}
else if (l % 2 != 0 && j % 2 != 0) {
l = l; j = (l - j) / 2;
}
else {
if (l % 2 == 0) { l = l / 2; j = j; }
else { l = l; j = j / 2; }
}
rem = l % j;
l = j;
j = rem;
return fasteuc(l, j);
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int a, b;
cin >> a >> b;
cout << fasteuc(a, b);
return 0;
}
--------------
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int a, b, l, j, rem;
cin >> a >> b;
if (a == b) { cout << a; return 0; }
if (a > b) { l = a; j = b; }
else { l = b; j = a; }
if (j == 0) { cout << l; return 0; }
if (l%j == 0) { cout << j; return 0; }
if (!(l % 2) && j % 2 == 0) {
l = l / 2; j = j / 2;
rem = l % j;
while (l % j) {
rem = l % j;
l = j;
j = rem;
}
cout << 2*rem;
}
else if (l % 2 != 0 && j % 2 != 0) {
l = l; j = (l - j) / 2;
rem = l % j;
while (l % j) {
rem = l % j;
l = j;
j = rem;
}
cout << rem;
}
else {
if (l % 2 == 0) { l = l / 2; j = j; }
else { l = l; j = j / 2; }
rem = l % j;
while (l % j) {
rem = l % j;
l = j;
j = rem;
}
cout << rem;
}
return 0;
}