update README.md for tasks 1 & 2
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# Problem RL -- рекурсия в цикл
|
||||
# Problem RL - рекурсия в цикл
|
||||
|
||||
Ниже приведён исходный код программы для рекурсивного алгоритма Евклида.
|
||||
|
||||
@@ -54,3 +54,55 @@ int main() {
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
# Решение
|
||||
|
||||
```c
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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;
|
||||
}
|
||||
```
|
||||
|
||||
63
2. problem_cf/README.md
Normal file
63
2. problem_cf/README.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# Problem CF - непрерывные дроби
|
||||
|
||||
Вам на вход приходят числитель и знаменатель дроби.
|
||||
|
||||
**Ваша задача**: написать программу выдающую на стандартный вывод представление цепной дробью.
|
||||
|
||||
## Пример 1
|
||||
|
||||
| Ввод | Вывод |
|
||||
| ------------- | -------------- |
|
||||
| `1 3` | `0 3` |
|
||||
|
||||
## Пример 2
|
||||
|
||||
| Ввод | Вывод |
|
||||
| ------------- | -------------- |
|
||||
| `43 19` | `2 3 1 4` |
|
||||
|
||||
## Пример 3
|
||||
|
||||
| Ввод | Вывод |
|
||||
| ------------- | -------------- |
|
||||
| `345 678` | `0 1 1 27 1 3` |
|
||||
|
||||
# Решение
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define ull unsigned long long
|
||||
|
||||
void cf(ull x, ull y);
|
||||
|
||||
int main() {
|
||||
ull x = 0, y = 0;
|
||||
int res;
|
||||
|
||||
res = scanf("%llu %llu", &x, &y);
|
||||
assert(res == 2);
|
||||
|
||||
cf(x, y);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cf(ull x, ull y) {
|
||||
ull a = x;
|
||||
ull b = y;
|
||||
|
||||
ull q = a / b;
|
||||
ull r = a % b;
|
||||
printf("%llu ", q);
|
||||
|
||||
while (r > 0) {
|
||||
a = b;
|
||||
b = r;
|
||||
q = a / b;
|
||||
r = a % b;
|
||||
printf("%llu ", q);
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -1,26 +1,3 @@
|
||||
/*
|
||||
Problem CF -- непрерывные дроби
|
||||
|
||||
Вам на вход приходят числитель и знаменатель дроби.
|
||||
|
||||
Ваша задача: написать программу выдающую на стандартный вывод представление цепной дробью.
|
||||
|
||||
Пример 1
|
||||
|
||||
Ввод: 1 3
|
||||
Вывод: 0 3
|
||||
|
||||
Пример 2
|
||||
|
||||
Ввод: 43 19
|
||||
Вывод: 2 3 1 4
|
||||
|
||||
Пример 3
|
||||
|
||||
Ввод: 345 678
|
||||
Вывод: 0 1 1 27 1 3
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
@@ -41,14 +18,9 @@ int main() {
|
||||
}
|
||||
|
||||
void cf(ull x, ull y) {
|
||||
// ull a = (x > y) ? x : y;
|
||||
// ull b = (x < y) ? x : y;
|
||||
|
||||
ull a = x;
|
||||
ull b = y;
|
||||
|
||||
// printf("a = %llu, b = %llu", a, b);
|
||||
|
||||
ull q = a / b;
|
||||
ull r = a % b;
|
||||
printf("%llu ", q);
|
||||
@@ -58,7 +30,6 @@ void cf(ull x, ull y) {
|
||||
b = r;
|
||||
q = a / b;
|
||||
r = a % b;
|
||||
// printf("a = %llu, b = %llu", a, b);
|
||||
printf("%llu ", q);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user