add tests for task1
This commit is contained in:
46
1. problem_rl/solution.cpp
Normal file
46
1. problem_rl/solution.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
|
||||
#define ull unsigned long long
|
||||
#define ll long long
|
||||
|
||||
ull gcd_cycle(ll x, ll y);
|
||||
ull gcd_recursive(ull x, ull y);
|
||||
|
||||
int main() {
|
||||
ull x = 0, y = 0, g;
|
||||
int res;
|
||||
|
||||
res = scanf("%llu %llu", &x, &y);
|
||||
assert(res == 2);
|
||||
g = gcd_cycle(x, y);
|
||||
printf("%llu\n", g);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ull gcd_recursive(ull x, ull y) {
|
||||
ull q;
|
||||
if (y > x)
|
||||
return gcd_recursive(y, x);
|
||||
assert (y > 0);
|
||||
q = x % y;
|
||||
if (q == 0)
|
||||
return y;
|
||||
return gcd_recursive(y, q);
|
||||
}
|
||||
|
||||
ull gcd_cycle(ll x, ll y) {
|
||||
x = llabs(x);
|
||||
y = llabs(y);
|
||||
|
||||
if (x == 0) return y;
|
||||
if (y == 0) return x;
|
||||
|
||||
while (y != 0) {
|
||||
ll r = x % y;
|
||||
x = y;
|
||||
y = r;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
Reference in New Issue
Block a user