diff --git a/1. problem_rl/README.md b/1. problem_rl/README.md index c7a725c..9a8ee8a 100644 --- a/1. problem_rl/README.md +++ b/1. problem_rl/README.md @@ -55,7 +55,7 @@ int main() { } ``` -# Решение +# Решение (C) ```c #include @@ -106,3 +106,5 @@ ull gcd_cycle(ll x, ll y) { return x; } ``` + +# Решение (C++) diff --git a/1. problem_rl/Solution.java b/1. problem_rl/Solution.java new file mode 100644 index 0000000..623a3b3 --- /dev/null +++ b/1. problem_rl/Solution.java @@ -0,0 +1,6 @@ +public class Solution { + public static void main(String[] args) { + + } + +} diff --git a/1. problem_rl/solution.c b/1. problem_rl/solution.c index 82cd405..494ea6b 100644 --- a/1. problem_rl/solution.c +++ b/1. problem_rl/solution.c @@ -1,57 +1,3 @@ -/* -Problem RL -- рекурсия в цикл - -Ниже приведён исходный код программы для рекурсивного алгоритма Евклида. - -Ваша задача: переписать его так, чтобы вместо рекурсии использовался цикл - -Посылка должна состоять из программы, считывающей со стандартного ввода два числа x и y и выводящей на стандартный вывод одно число g - -Пример 1 - -Ввод: 2 1 -Вывод: 1 - -Пример 2 - -Ввод: 3 2 -Вывод: 1 - -Пример 3 - -Ввод: 2 4 -Вывод: 2 - -Примечания - -#include -#include -#include - -unsigned long long gcd(unsigned long long x, - unsigned long long y) { - unsigned long long q; - if (y > x) - return gcd(y, x); - assert (y > 0); - q = x % y; - if (q == 0) - return y; - return gcd(y, q); -} - -int main() { - unsigned long long x = 0, y = 0, g; - int res; - - res = scanf("%llu %llu", &x, &y); - assert(res == 2); - g = gcd(x, y); - printf("%llu\n", g); - return 0; -} -*/ - #include #include #include diff --git a/1. problem_rl/solution.cpp b/1. problem_rl/solution.cpp new file mode 100644 index 0000000..66f824d --- /dev/null +++ b/1. problem_rl/solution.cpp @@ -0,0 +1,46 @@ +#include +#include + +#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; +} diff --git a/1. problem_rl/solution.py b/1. problem_rl/solution.py new file mode 100644 index 0000000..e69de29 diff --git a/1. problem_rl/test.py b/1. problem_rl/test.py new file mode 100755 index 0000000..6f5a444 --- /dev/null +++ b/1. problem_rl/test.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +from pathlib import Path +from subprocess import run + +tests_1: dict = { + "2 1" : "1", + "3 2" : "1", + "2 4" : "2", + "1769 427" : "61", + "698917 -102089" : "7853", + "917 872" : "1", + "-417143 -455947" : "9701", + "63862 -55298" : "2", + "702228 373125" : "3", + "-659323 727699" : "7" +} + +def test(file_name: str, compile_cmd: str, run_cmd: list[str], tests_dict: dict): + + print(f"=== TESTING {file_name.upper()} ===") + file_path = Path(file_name) + + if not file_path.is_file(): + print(f"file '{file_name}' was not found.") + return + + print(f"{file_name} found") + run([compile_cmd, file_name], check=True) + + for t, res in tests_dict.items(): + out = run( + run_cmd, + input=t, + text=True, + capture_output=True + ) + + program_result = out.stdout.strip() + + if program_result != res: + print("test failed:") + print(f"\ttest: {t}") + print(f"\tprogram result: {program_result}") + print(f"\tcorrect result: {res}") + break + else: + print("test passed:") + print(f"\ttest: {t}") + print(f"\tresult: {res}") + else: + print("all tests passed!") + + + +def main(): + test("solution.c", "gcc", ["./a.out"], tests_1) + test("solution.cpp", "g++", ["./a.out"], tests_1) + +if __name__ == "__main__": + main() + + diff --git a/3. problem_ee/README.md b/3. problem_ee/README.md new file mode 100644 index 0000000..018b7a5 --- /dev/null +++ b/3. problem_ee/README.md @@ -0,0 +1,37 @@ +# Problem EE - расширенный алгоритм E + +Даны два целых числа `x` и `y` + +Необходимо найти их наибольший общий делитель `d` и целые, возможно отрицательные числа `a` и `b`, такие, что: + +``` +ax + by = d +``` + +Используйте тип `long long` для всех чисел + +Посылка должна состоять из программы, считывающей со стандартного ввода два числа `x` и `y` и выводящей на стандартный вывод три числа: `a`, `b`, `d`. + +## Пример 1 + +| Ввод | Вывод | +| ------------- | -------------- | +| `2 1` | `0 1 1` | + +## Пример 2 + +| Ввод | Вывод | +| ------------- | -------------- | +| `3 2` | `1 -1 1` | + +## Пример 3 + +| Ввод | Вывод | +| ------------- | -------------- | +| `2 4` | `1 0 2` | + +# Решение + +```c + +``` diff --git a/3. problem_ee/solution.c b/3. problem_ee/solution.c new file mode 100644 index 0000000..d2dd184 --- /dev/null +++ b/3. problem_ee/solution.c @@ -0,0 +1,50 @@ +#include +#include +#include + +#define ll long long + +void ee(ll x, ll y); + +int main(int argc, char *argv[]) +{ + ll x = 0, y = 0; + int res; + + + res = scanf("%lld %lld", &x, &y); + assert(res == 2); + + ee(x, y); + + return EXIT_SUCCESS; +} + +void ee(ll x, ll y) { + ll x0 = 1, y0 = 0; + ll x1 = 0, y1 = 1; + + while (y != 0) { + ll q = x / y; + + ll t = x % y; + x = y; + y = t; + + t = x1; + x1 = x0 - q * x1; + x0 = t; + + t = y1; + y1 = y0 - q * y1; + y0 = t; + } + + if (x < 0) { + x = -x; + x0 = -x0; + y0 = -y0; + } + + printf("%lld %lld %lld\n", x0, y0, x); +} diff --git a/README.md b/README.md index 11954f8..cd4e89f 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,7 @@ here i've got various tasks (w/ solutions) from C contest | ---------- | -------- | | 1 | [*problem_rl*](1.%20problem_rl) | | 2 | [*problem_cf*](2.%20problem_cf) | +| 3 | [*problem_ee*](3.%20problem_ee) | +