add tests for task1

This commit is contained in:
2026-01-29 23:15:45 +05:00
parent 0658a15911
commit 1e5dbe95ec
9 changed files with 207 additions and 55 deletions

View File

@@ -55,7 +55,7 @@ int main() {
}
```
# Решение
# Решение (C)
```c
#include <assert.h>
@@ -106,3 +106,5 @@ ull gcd_cycle(ll x, ll y) {
return x;
}
```
# Решение (C++)

View File

@@ -0,0 +1,6 @@
public class Solution {
public static void main(String[] args) {
}
}

View File

@@ -1,57 +1,3 @@
/*
Problem RL -- рекурсия в цикл
Ниже приведён исходный код программы для рекурсивного алгоритма Евклида.
Ваша задача: переписать его так, чтобы вместо рекурсии использовался цикл
Посылка должна состоять из программы, считывающей со стандартного ввода два числа x и y и выводящей на стандартный вывод одно число g
Пример 1
Ввод: 2 1
Вывод: 1
Пример 2
Ввод: 3 2
Вывод: 1
Пример 3
Ввод: 2 4
Вывод: 2
Примечания
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
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 <assert.h>
#include <stdio.h>
#include <stdlib.h>

View 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;
}

View File

63
1. problem_rl/test.py Executable file
View File

@@ -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()

37
3. problem_ee/README.md Normal file
View File

@@ -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
```

50
3. problem_ee/solution.c Normal file
View File

@@ -0,0 +1,50 @@
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#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);
}

View File

@@ -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) |