From b2a09c3542bee4073ec9b396c1a80cc448d0a216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Mon, 20 Oct 2025 10:06:58 +0200 Subject: [PATCH 01/12] hw-02: Assignment definition --- homework-02-rail-building/README.md | 261 ++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 homework-02-rail-building/README.md diff --git a/homework-02-rail-building/README.md b/homework-02-rail-building/README.md new file mode 100644 index 0000000..838c731 --- /dev/null +++ b/homework-02-rail-building/README.md @@ -0,0 +1,261 @@ +Úkolem je vytvořit program, který bude počítat a zobrazovat počty traťových dílců potřebných pro stavbu trati zadané délky. + +Předpokládáme, že výrobce dodává traťové dílce dvou velikostí. Úkolem je určit počty potřebných traťových dílců jednotlivých velikostí, které jsou potřebné pro výstavbu trati zadané délky. Při stavbě trati musíme používat dílce vždy celé (nelze je krátit). + +Vstupem programu je zadání délek vyráběných traťových dílců. Jedná se o dvě navzájem různá celá čísla. Po tomto vstupu následuje zadání délky trati ke stavbě. Pro zadanou délku trati program vypočte potřebné množství traťových dílců. Zadání délky je navíc dvojího druhu: + +- \- délka pro toto zadání program zobrazí kolika různými způsoby lze traťové dílce kombinovat, +- \+ délka pro toto zadání program vypíše nalezené kombinace a zobrazí jejich počet. + +Výstupem programu je počet možných způsobů, kterými lze zkombinovat traťové dílce pro zadanou vzdálenost. Podle vstupu (znaku + či - v zadání) se dále zobrazí či nezobrazí i výpis jednotlivých možných kombinací. Přesný formát výstupu je v ukázce níže. Pokud zadanou délku nelze z vyráběných dílců sestavit, program pro zadaný vstup vypíše informaci podle ukázky. Pozor, za výpisem je odřádkování (\n). + +Pokud je vstup neplatný, program to musí detekovat, zobrazit chybové hlášení a ukončit se. Chybové hlášení zobrazujte na standardní výstup (ne na chybový výstup). Za chybu považujte: + +- délky vyráběných traťových dílců jsou nečíselné, záporné, nulové nebo jsou obě délky shodné, +- při zadávání délky nebyl použit ani znak + ani - pro zapnutí/vypnutí úplného výpisu všech kombinací, +- zadání délky trati je nečíselné nebo záporné. + +Program v základní verzi pracuje s malými čísly, tedy k vyřešení stačí naivní algoritmus kontrolující přípustné možnosti. Pokud má být zobrazován kompletní výpis všech možných kombinací, nelze algoritmus příliš vylepšovat. Úloha nabízí bonusový test. V tomto testu se testuje, zda řešení dokáže rychle počítat počty možných kombinací (pracuje pouze v režimu -, tedy bez výpisu všech nalezených kombinací). Pokud bonusový test zvládnete, dostane řešení více než nominálních 100% bodů. Pokud v programu použijete pouze naivní algoritmus, bonusovým testem neprojdete a získáte nejvýše nominální hodnocení 100% bodů. + +Při programování si dejte pozor na přesnou podobu výpisů. Výstup Vašeho programu kontroluje stroj, který požaduje přesnou shodu výstupů Vašeho programu s výstupy referenčními. Za chybu je považováno, pokud se výpis liší. I chybějící nebo přebývající mezera/odřádkování je považováno za chybu. Abyste tyto problémy rychle vyloučili, použijte přiložený archiv se sadou vstupních a očekávaných výstupních dat. + +Váš program bude spouštěn v omezeném testovacím prostředí. Je omezen dobou běhu (limit je vidět v logu referenčního řešení) a dále je omezena i velikost dostupné paměti (ale tato úloha by ani s jedním omezením neměla mít v povinných testech problém). Zvládnutí bonusového testu již vyžaduje lepší algoritmus. + +**Ukázka práce programu:** + +``` +Delky koleji: +10 13 +Vzdalenost: ++ 100 += 10 * 10 + 13 * 0 +Celkem variant: 1 +``` + +--- + +``` +Delky koleji: +10 13 +Vzdalenost: ++ 749 += 10 * 32 + 13 * 33 += 10 * 19 + 13 * 43 += 10 * 6 + 13 * 53 += 10 * 71 + 13 * 3 += 10 * 58 + 13 * 13 += 10 * 45 + 13 * 23 +Celkem variant: 6 +``` + +--- + +``` +Delky koleji: +10 13 +Vzdalenost: ++ 71 +Reseni neexistuje. +``` + +--- + +``` +Delky koleji: +10 13 +Vzdalenost: ++ 0 += 10 * 0 + 13 * 0 +Celkem variant: 1 +``` + +--- + +``` +Delky koleji: +54 16 +Vzdalenost: ++ 121 +Reseni neexistuje. +``` + +--- + +``` +Delky koleji: +54 16 +Vzdalenost: ++ 2400 += 54 * 16 + 16 * 96 += 54 * 8 + 16 * 123 += 54 * 0 + 16 * 150 += 54 * 40 + 16 * 15 += 54 * 32 + 16 * 42 += 54 * 24 + 16 * 69 +Celkem variant: 6 +``` + +--- + +``` +Delky koleji: +173 459 +Vzdalenost: +- 5219 +Reseni neexistuje. +``` + +--- + +``` +Delky koleji: +173 459 +Vzdalenost: +- 234875 +Celkem variant: 3 +``` + +--- + +``` +Delky koleji: +22 33 +Vzdalenost: +* 12 +Nespravny vstup. +``` + +--- + +``` +Delky koleji: +10 abc +Nespravny vstup. +``` + +**Poznámky:** + +- Ukázkové běhy zachycují očekávané výpisy Vašeho programu (tučné písmo) a vstupy zadané uživatelem (základní písmo). Zvýraznění tučným písmem je použité pouze zde na stránce zadání, aby byl výpis lépe čitelný. Váš program má za úkol pouze zobrazit text bez zvýrazňování (bez HTML markupu). +- Znak odřádkování (\n) je i za poslední řádkou výstupu (i za případným chybovým hlášením). +- Výpis nalezených kombinací (pokud je požadován) nemá určené pořadí. Testovací prostředí si v případě potřeby pořadí před porovnáním prohodí. Tedy například pro dílce délky 13 17 a požadovanou délku tratě 7111 jsou za správné považované následující výpisy: + +``` += 13 * 190 + 17 * 273 += 13 * 428 + 17 * 91 += 13 * 105 + 17 * 338 += 13 * 343 + 17 * 156 += 13 * 20 + 17 * 403 += 13 * 258 + 17 * 221 += 13 * 496 + 17 * 39 += 13 * 173 + 17 * 286 += 13 * 411 + 17 * 104 += 13 * 88 + 17 * 351 += 13 * 326 + 17 * 169 += 13 * 3 + 17 * 416 += 13 * 241 + 17 * 234 += 13 * 479 + 17 * 52 += 13 * 156 + 17 * 299 += 13 * 394 + 17 * 117 += 13 * 71 + 17 * 364 += 13 * 309 + 17 * 182 += 13 * 547 + 17 * 0 += 13 * 224 + 17 * 247 += 13 * 462 + 17 * 65 += 13 * 139 + 17 * 312 += 13 * 377 + 17 * 130 += 13 * 54 + 17 * 377 += 13 * 292 + 17 * 195 += 13 * 530 + 17 * 13 += 13 * 207 + 17 * 260 += 13 * 445 + 17 * 78 += 13 * 122 + 17 * 325 += 13 * 360 + 17 * 143 += 13 * 37 + 17 * 390 += 13 * 275 + 17 * 208 += 13 * 513 + 17 * 26 +Celkem variant: 33 +``` + +--- + +``` + += 13 * 428 + 17 * 91 += 13 * 190 + 17 * 273 += 13 * 105 + 17 * 338 += 13 * 343 + 17 * 156 += 13 * 20 + 17 * 403 += 13 * 258 + 17 * 221 += 13 * 496 + 17 * 39 += 13 * 173 + 17 * 286 += 13 * 411 + 17 * 104 += 13 * 88 + 17 * 351 += 13 * 326 + 17 * 169 += 13 * 3 + 17 * 416 += 13 * 241 + 17 * 234 += 13 * 479 + 17 * 52 += 13 * 156 + 17 * 299 += 13 * 394 + 17 * 117 += 13 * 71 + 17 * 364 += 13 * 309 + 17 * 182 += 13 * 547 + 17 * 0 += 13 * 224 + 17 * 247 += 13 * 462 + 17 * 65 += 13 * 139 + 17 * 312 += 13 * 377 + 17 * 130 += 13 * 54 + 17 * 377 += 13 * 292 + 17 * 195 += 13 * 530 + 17 * 13 += 13 * 207 + 17 * 260 += 13 * 445 + 17 * 78 += 13 * 122 + 17 * 325 += 13 * 360 + 17 * 143 += 13 * 37 + 17 * 390 += 13 * 275 + 17 * 208 += 13 * 513 + 17 * 26 +Celkem variant: 33 +``` + +--- + +``` += 13 * 20 + 17 * 403 += 13 * 428 + 17 * 91 += 13 * 190 + 17 * 273 += 13 * 105 + 17 * 338 += 13 * 343 + 17 * 156 += 13 * 258 + 17 * 221 += 13 * 496 + 17 * 39 += 13 * 173 + 17 * 286 += 13 * 411 + 17 * 104 += 13 * 88 + 17 * 351 += 13 * 326 + 17 * 169 += 13 * 3 + 17 * 416 += 13 * 241 + 17 * 234 += 13 * 479 + 17 * 52 += 13 * 156 + 17 * 299 += 13 * 394 + 17 * 117 += 13 * 71 + 17 * 364 += 13 * 309 + 17 * 182 += 13 * 547 + 17 * 0 += 13 * 224 + 17 * 247 += 13 * 462 + 17 * 65 += 13 * 139 + 17 * 312 += 13 * 377 + 17 * 130 += 13 * 54 + 17 * 377 += 13 * 292 + 17 * 195 += 13 * 530 + 17 * 13 += 13 * 207 + 17 * 260 += 13 * 445 + 17 * 78 += 13 * 122 + 17 * 325 += 13 * 360 + 17 * 143 += 13 * 37 + 17 * 390 += 13 * 275 + 17 * 208 += 13 * 513 + 17 * 26 +Celkem variant: 33 +``` +a libovolná další ze zbývajících 8683317618811886495518194401279999997 permutací. +- Při kontrole výstupů Vašeho programu nelze přímo porovnávat Váš výpis a referenční výpis. Před porovnáním budete nejspíše muset upravit pořadí nalezených kombinací. Odpovídající nástroje jsou probírané v BI-UOS. +- Při programování si dejte pozor na rozsah použitých datových typů. Pro povinné testy lze vystačit s datovým typem int. Pokud chcete projít i bonusovým testem, je potřeba používat datový typ s větším rozsahem (long long). +- Rozsah datových typů lze snadno překročit i nevhodným pořadím prováděných operací. Pokud například potřrbujete spočítat výraz +- Pro načtení znaménka +/- ze vstupu se hodí formát "%c" nebo " %c". Rozdíl mezi těmito konverzemi je dobře popsaný v manuálu k funkci scanf. From 053e6da8591a31069c9acd085570c2ca5245cef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Mon, 20 Oct 2025 10:09:21 +0200 Subject: [PATCH 02/12] hw-02: Test data --- homework-02-rail-building/test_data/0000_in.txt | 2 ++ homework-02-rail-building/test_data/0000_out.txt | 4 ++++ homework-02-rail-building/test_data/0000_out_win.txt | 4 ++++ homework-02-rail-building/test_data/0001_in.txt | 2 ++ homework-02-rail-building/test_data/0001_out.txt | 9 +++++++++ homework-02-rail-building/test_data/0001_out_win.txt | 9 +++++++++ homework-02-rail-building/test_data/0002_in.txt | 2 ++ homework-02-rail-building/test_data/0002_out.txt | 3 +++ homework-02-rail-building/test_data/0002_out_win.txt | 3 +++ homework-02-rail-building/test_data/0003_in.txt | 2 ++ homework-02-rail-building/test_data/0003_out.txt | 4 ++++ homework-02-rail-building/test_data/0003_out_win.txt | 4 ++++ homework-02-rail-building/test_data/0004_in.txt | 2 ++ homework-02-rail-building/test_data/0004_out.txt | 3 +++ homework-02-rail-building/test_data/0004_out_win.txt | 3 +++ homework-02-rail-building/test_data/0005_in.txt | 2 ++ homework-02-rail-building/test_data/0005_out.txt | 9 +++++++++ homework-02-rail-building/test_data/0005_out_win.txt | 9 +++++++++ homework-02-rail-building/test_data/0006_in.txt | 2 ++ homework-02-rail-building/test_data/0006_out.txt | 3 +++ homework-02-rail-building/test_data/0006_out_win.txt | 3 +++ homework-02-rail-building/test_data/0007_in.txt | 2 ++ homework-02-rail-building/test_data/0007_out.txt | 3 +++ homework-02-rail-building/test_data/0007_out_win.txt | 3 +++ homework-02-rail-building/test_data/0008_in.txt | 2 ++ homework-02-rail-building/test_data/0008_out.txt | 3 +++ homework-02-rail-building/test_data/0008_out_win.txt | 3 +++ homework-02-rail-building/test_data/0009_in.txt | 1 + homework-02-rail-building/test_data/0009_out.txt | 2 ++ homework-02-rail-building/test_data/0009_out_win.txt | 2 ++ 30 files changed, 105 insertions(+) create mode 100644 homework-02-rail-building/test_data/0000_in.txt create mode 100644 homework-02-rail-building/test_data/0000_out.txt create mode 100644 homework-02-rail-building/test_data/0000_out_win.txt create mode 100644 homework-02-rail-building/test_data/0001_in.txt create mode 100644 homework-02-rail-building/test_data/0001_out.txt create mode 100644 homework-02-rail-building/test_data/0001_out_win.txt create mode 100644 homework-02-rail-building/test_data/0002_in.txt create mode 100644 homework-02-rail-building/test_data/0002_out.txt create mode 100644 homework-02-rail-building/test_data/0002_out_win.txt create mode 100644 homework-02-rail-building/test_data/0003_in.txt create mode 100644 homework-02-rail-building/test_data/0003_out.txt create mode 100644 homework-02-rail-building/test_data/0003_out_win.txt create mode 100644 homework-02-rail-building/test_data/0004_in.txt create mode 100644 homework-02-rail-building/test_data/0004_out.txt create mode 100644 homework-02-rail-building/test_data/0004_out_win.txt create mode 100644 homework-02-rail-building/test_data/0005_in.txt create mode 100644 homework-02-rail-building/test_data/0005_out.txt create mode 100644 homework-02-rail-building/test_data/0005_out_win.txt create mode 100644 homework-02-rail-building/test_data/0006_in.txt create mode 100644 homework-02-rail-building/test_data/0006_out.txt create mode 100644 homework-02-rail-building/test_data/0006_out_win.txt create mode 100644 homework-02-rail-building/test_data/0007_in.txt create mode 100644 homework-02-rail-building/test_data/0007_out.txt create mode 100644 homework-02-rail-building/test_data/0007_out_win.txt create mode 100644 homework-02-rail-building/test_data/0008_in.txt create mode 100644 homework-02-rail-building/test_data/0008_out.txt create mode 100644 homework-02-rail-building/test_data/0008_out_win.txt create mode 100644 homework-02-rail-building/test_data/0009_in.txt create mode 100644 homework-02-rail-building/test_data/0009_out.txt create mode 100644 homework-02-rail-building/test_data/0009_out_win.txt diff --git a/homework-02-rail-building/test_data/0000_in.txt b/homework-02-rail-building/test_data/0000_in.txt new file mode 100644 index 0000000..2b72080 --- /dev/null +++ b/homework-02-rail-building/test_data/0000_in.txt @@ -0,0 +1,2 @@ +10 13 ++ 100 diff --git a/homework-02-rail-building/test_data/0000_out.txt b/homework-02-rail-building/test_data/0000_out.txt new file mode 100644 index 0000000..6367851 --- /dev/null +++ b/homework-02-rail-building/test_data/0000_out.txt @@ -0,0 +1,4 @@ +Delky koleji: +Vzdalenost: += 10 * 10 + 13 * 0 +Celkem variant: 1 diff --git a/homework-02-rail-building/test_data/0000_out_win.txt b/homework-02-rail-building/test_data/0000_out_win.txt new file mode 100644 index 0000000..47e571d --- /dev/null +++ b/homework-02-rail-building/test_data/0000_out_win.txt @@ -0,0 +1,4 @@ +Delky koleji: +Vzdalenost: += 10 * 10 + 13 * 0 +Celkem variant: 1 diff --git a/homework-02-rail-building/test_data/0001_in.txt b/homework-02-rail-building/test_data/0001_in.txt new file mode 100644 index 0000000..29c50d9 --- /dev/null +++ b/homework-02-rail-building/test_data/0001_in.txt @@ -0,0 +1,2 @@ +10 13 ++ 749 diff --git a/homework-02-rail-building/test_data/0001_out.txt b/homework-02-rail-building/test_data/0001_out.txt new file mode 100644 index 0000000..df01575 --- /dev/null +++ b/homework-02-rail-building/test_data/0001_out.txt @@ -0,0 +1,9 @@ +Delky koleji: +Vzdalenost: += 10 * 32 + 13 * 33 += 10 * 19 + 13 * 43 += 10 * 6 + 13 * 53 += 10 * 71 + 13 * 3 += 10 * 58 + 13 * 13 += 10 * 45 + 13 * 23 +Celkem variant: 6 diff --git a/homework-02-rail-building/test_data/0001_out_win.txt b/homework-02-rail-building/test_data/0001_out_win.txt new file mode 100644 index 0000000..ee8a83a --- /dev/null +++ b/homework-02-rail-building/test_data/0001_out_win.txt @@ -0,0 +1,9 @@ +Delky koleji: +Vzdalenost: += 10 * 32 + 13 * 33 += 10 * 19 + 13 * 43 += 10 * 6 + 13 * 53 += 10 * 71 + 13 * 3 += 10 * 58 + 13 * 13 += 10 * 45 + 13 * 23 +Celkem variant: 6 diff --git a/homework-02-rail-building/test_data/0002_in.txt b/homework-02-rail-building/test_data/0002_in.txt new file mode 100644 index 0000000..318f884 --- /dev/null +++ b/homework-02-rail-building/test_data/0002_in.txt @@ -0,0 +1,2 @@ +10 13 ++ 71 diff --git a/homework-02-rail-building/test_data/0002_out.txt b/homework-02-rail-building/test_data/0002_out.txt new file mode 100644 index 0000000..5f7fc66 --- /dev/null +++ b/homework-02-rail-building/test_data/0002_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0002_out_win.txt b/homework-02-rail-building/test_data/0002_out_win.txt new file mode 100644 index 0000000..6edad24 --- /dev/null +++ b/homework-02-rail-building/test_data/0002_out_win.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0003_in.txt b/homework-02-rail-building/test_data/0003_in.txt new file mode 100644 index 0000000..cb59810 --- /dev/null +++ b/homework-02-rail-building/test_data/0003_in.txt @@ -0,0 +1,2 @@ +10 13 ++ 0 diff --git a/homework-02-rail-building/test_data/0003_out.txt b/homework-02-rail-building/test_data/0003_out.txt new file mode 100644 index 0000000..82b227f --- /dev/null +++ b/homework-02-rail-building/test_data/0003_out.txt @@ -0,0 +1,4 @@ +Delky koleji: +Vzdalenost: += 10 * 0 + 13 * 0 +Celkem variant: 1 diff --git a/homework-02-rail-building/test_data/0003_out_win.txt b/homework-02-rail-building/test_data/0003_out_win.txt new file mode 100644 index 0000000..e6bb688 --- /dev/null +++ b/homework-02-rail-building/test_data/0003_out_win.txt @@ -0,0 +1,4 @@ +Delky koleji: +Vzdalenost: += 10 * 0 + 13 * 0 +Celkem variant: 1 diff --git a/homework-02-rail-building/test_data/0004_in.txt b/homework-02-rail-building/test_data/0004_in.txt new file mode 100644 index 0000000..56a3a24 --- /dev/null +++ b/homework-02-rail-building/test_data/0004_in.txt @@ -0,0 +1,2 @@ +54 16 ++ 121 diff --git a/homework-02-rail-building/test_data/0004_out.txt b/homework-02-rail-building/test_data/0004_out.txt new file mode 100644 index 0000000..5f7fc66 --- /dev/null +++ b/homework-02-rail-building/test_data/0004_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0004_out_win.txt b/homework-02-rail-building/test_data/0004_out_win.txt new file mode 100644 index 0000000..6edad24 --- /dev/null +++ b/homework-02-rail-building/test_data/0004_out_win.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0005_in.txt b/homework-02-rail-building/test_data/0005_in.txt new file mode 100644 index 0000000..b120afc --- /dev/null +++ b/homework-02-rail-building/test_data/0005_in.txt @@ -0,0 +1,2 @@ +54 16 ++ 2400 diff --git a/homework-02-rail-building/test_data/0005_out.txt b/homework-02-rail-building/test_data/0005_out.txt new file mode 100644 index 0000000..039b1f3 --- /dev/null +++ b/homework-02-rail-building/test_data/0005_out.txt @@ -0,0 +1,9 @@ +Delky koleji: +Vzdalenost: += 54 * 16 + 16 * 96 += 54 * 8 + 16 * 123 += 54 * 0 + 16 * 150 += 54 * 40 + 16 * 15 += 54 * 32 + 16 * 42 += 54 * 24 + 16 * 69 +Celkem variant: 6 diff --git a/homework-02-rail-building/test_data/0005_out_win.txt b/homework-02-rail-building/test_data/0005_out_win.txt new file mode 100644 index 0000000..21f4461 --- /dev/null +++ b/homework-02-rail-building/test_data/0005_out_win.txt @@ -0,0 +1,9 @@ +Delky koleji: +Vzdalenost: += 54 * 16 + 16 * 96 += 54 * 8 + 16 * 123 += 54 * 0 + 16 * 150 += 54 * 40 + 16 * 15 += 54 * 32 + 16 * 42 += 54 * 24 + 16 * 69 +Celkem variant: 6 diff --git a/homework-02-rail-building/test_data/0006_in.txt b/homework-02-rail-building/test_data/0006_in.txt new file mode 100644 index 0000000..286bc5b --- /dev/null +++ b/homework-02-rail-building/test_data/0006_in.txt @@ -0,0 +1,2 @@ +173 459 +- 5219 diff --git a/homework-02-rail-building/test_data/0006_out.txt b/homework-02-rail-building/test_data/0006_out.txt new file mode 100644 index 0000000..5f7fc66 --- /dev/null +++ b/homework-02-rail-building/test_data/0006_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0006_out_win.txt b/homework-02-rail-building/test_data/0006_out_win.txt new file mode 100644 index 0000000..6edad24 --- /dev/null +++ b/homework-02-rail-building/test_data/0006_out_win.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0007_in.txt b/homework-02-rail-building/test_data/0007_in.txt new file mode 100644 index 0000000..1882110 --- /dev/null +++ b/homework-02-rail-building/test_data/0007_in.txt @@ -0,0 +1,2 @@ +173 459 +- 234875 diff --git a/homework-02-rail-building/test_data/0007_out.txt b/homework-02-rail-building/test_data/0007_out.txt new file mode 100644 index 0000000..32fc2da --- /dev/null +++ b/homework-02-rail-building/test_data/0007_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Celkem variant: 3 diff --git a/homework-02-rail-building/test_data/0007_out_win.txt b/homework-02-rail-building/test_data/0007_out_win.txt new file mode 100644 index 0000000..c61ffcc --- /dev/null +++ b/homework-02-rail-building/test_data/0007_out_win.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Celkem variant: 3 diff --git a/homework-02-rail-building/test_data/0008_in.txt b/homework-02-rail-building/test_data/0008_in.txt new file mode 100644 index 0000000..0d6c26c --- /dev/null +++ b/homework-02-rail-building/test_data/0008_in.txt @@ -0,0 +1,2 @@ +22 33 +* 12 diff --git a/homework-02-rail-building/test_data/0008_out.txt b/homework-02-rail-building/test_data/0008_out.txt new file mode 100644 index 0000000..529756d --- /dev/null +++ b/homework-02-rail-building/test_data/0008_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Nespravny vstup. diff --git a/homework-02-rail-building/test_data/0008_out_win.txt b/homework-02-rail-building/test_data/0008_out_win.txt new file mode 100644 index 0000000..fdcbde6 --- /dev/null +++ b/homework-02-rail-building/test_data/0008_out_win.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Nespravny vstup. diff --git a/homework-02-rail-building/test_data/0009_in.txt b/homework-02-rail-building/test_data/0009_in.txt new file mode 100644 index 0000000..6e1bd6e --- /dev/null +++ b/homework-02-rail-building/test_data/0009_in.txt @@ -0,0 +1 @@ +10 abc diff --git a/homework-02-rail-building/test_data/0009_out.txt b/homework-02-rail-building/test_data/0009_out.txt new file mode 100644 index 0000000..f48d3de --- /dev/null +++ b/homework-02-rail-building/test_data/0009_out.txt @@ -0,0 +1,2 @@ +Delky koleji: +Nespravny vstup. diff --git a/homework-02-rail-building/test_data/0009_out_win.txt b/homework-02-rail-building/test_data/0009_out_win.txt new file mode 100644 index 0000000..ab2ea84 --- /dev/null +++ b/homework-02-rail-building/test_data/0009_out_win.txt @@ -0,0 +1,2 @@ +Delky koleji: +Nespravny vstup. From 3a9f6502f0af3f81cb9602d8fb2560b784b71f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Mon, 20 Oct 2025 17:08:27 +0200 Subject: [PATCH 03/12] ex-05: Assignment definition --- exercise-05-calculator/README.md | 84 ++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 exercise-05-calculator/README.md diff --git a/exercise-05-calculator/README.md b/exercise-05-calculator/README.md new file mode 100644 index 0000000..2059f72 --- /dev/null +++ b/exercise-05-calculator/README.md @@ -0,0 +1,84 @@ +Úkolem je vytvořit program, který bude zpracovávat jednoduché matematické vzorce. + +Na vstupu dostanete dvě desetinná čísla oddělená znakem operace a zakončená znakem rovná se. Vaším úkolem je vypočítat hodnotu podle zadaného operátoru: + +- pro + určit součet, +- pro - určit rozdíl, +- pro * určit součin, +- pro / určit celočíselný podíl (např. 3.5 / 1.5 = 2, kolikrát se do dělence vejde celý dělitel). + +Jiné operace program nebude umět a vypíše pro ně chybové hlášení. Formát vstupu a výstupu je vidět níže na ukázce práce programu. + +Pokud je vstup neplatný, program to musí detekovat a zobrazit chybové hlášení. Chybové hlášení zobrazujte na standardní výstup (ne na chybový výstup). Za chybu považujte: + +- zadané číslo není validní desetinné číslo, +- dělitel je 0 pro operaci /, +- čísla nejsou oddělena validním operátorem (+, -, * nebo /), +- za posledním číslem se nenachází znak =. + +**Ukázka práce programu:** + +``` +Zadejte vzorec: +3.5 + 1.5 = +5 +``` + +--- + +``` +Zadejte vzorec: +3.5 / 1.5 = +2 +``` + +--- + +``` +Zadejte vzorec: +1000000 * 1.5e+8 = +1.5e+14 +``` + +--- + +``` +Zadejte vzorec: +7 / -1 = +-7 +``` + +--- + +``` +Zadejte vzorec: +abc / 1 = +Nespravny vstup. +``` + +--- + +``` +Zadejte vzorec: +4 & 10 = +Nespravny vstup. +``` + +Poznámky: + +- Ukázkové běhy zachycují očekávané výpisy Vašeho programu (tučné písmo) a vstupy zadané uživatelem (základní písmo). Zvýraznění tučným písmem je použité pouze zde na stránce zadání, aby byl výpis lépe čitelný. Váš program má za úkol zobrazit text bez zvýrazňování (bez HTML markupu). +- Znak odřádkování (\n) je i za poslední řádkou výstupu (i za případným chybovým hlášením). +- Pro reprezentaci hodnot použijte desetinná čísla typu double. Nepoužívejte typ float, jeho přesnost nemusí být dostatečná. +- Při programování si dejte pozor na přesnou podobu výpisů. Výstup Vašeho programu kontroluje stroj, který požaduje přesnou shodu výstupů Vašeho programu s výstupy referenčními. Za chybu je považováno, pokud se výpis liší. I chybějící nebo přebývající mezera/odřádkování je považováno za chybu. Abyste tyto problémy rychle vyloučili, použijte přiložený archiv se sadou vstupních a očekávaných výstupních dat. Podívejte se na videotutoriál (Courses -> Video tutoriály), jak testovací data použít a jak testování zautomatizovat. +- Výsledná hodnota je porovnávána jako desetinné číslo s tolerancí 1 promile. +- Váš program bude spouštěn v omezeném testovacím prostředí. Je omezen dobou běhu (limit je vidět v logu referenčního řešení) a dále je omezena i velikost dostupné paměti (ale tato úloha by ani s jedním omezením neměla mít problém). +- Slovní popis struktury platných vstupních dat není zcela exaktní. Proto připojujeme i formální popis vstupního jazyka v EBNF: +``` +input ::= { whiteSpace } decimal { whiteSpace } op { whiteSpace } decimal { whiteSpace } '=' +whiteSpace ::= ' ' | '\t' | '\n' | '\r' +decimal ::= [ '+' | '-' ] integer [ '.' integer [ ( 'e' | 'E' ) [ '+' | '-' ] integer ] ] | + [ '+' | '-' ] '.' integer [ ( 'e' | 'E' ) [ '+' | '-' ] integer ] +integer ::= digit { digit } +digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' +op ::= '+' | '-' | '*' | '/' +``` From bdd72382c3305938ce11e144a2fc0dba6fcb7acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Mon, 20 Oct 2025 17:09:03 +0200 Subject: [PATCH 04/12] ex-05: Test data --- exercise-05-calculator/test_data/0000_in.txt | 1 + exercise-05-calculator/test_data/0000_out.txt | 2 ++ exercise-05-calculator/test_data/0000_out_win.txt | 2 ++ exercise-05-calculator/test_data/0001_in.txt | 1 + exercise-05-calculator/test_data/0001_out.txt | 2 ++ exercise-05-calculator/test_data/0001_out_win.txt | 2 ++ exercise-05-calculator/test_data/0002_in.txt | 1 + exercise-05-calculator/test_data/0002_out.txt | 2 ++ exercise-05-calculator/test_data/0002_out_win.txt | 2 ++ exercise-05-calculator/test_data/0003_in.txt | 1 + exercise-05-calculator/test_data/0003_out.txt | 2 ++ exercise-05-calculator/test_data/0003_out_win.txt | 2 ++ exercise-05-calculator/test_data/0004_in.txt | 1 + exercise-05-calculator/test_data/0004_out.txt | 2 ++ exercise-05-calculator/test_data/0004_out_win.txt | 2 ++ exercise-05-calculator/test_data/0005_in.txt | 1 + exercise-05-calculator/test_data/0005_out.txt | 2 ++ exercise-05-calculator/test_data/0005_out_win.txt | 2 ++ 18 files changed, 30 insertions(+) create mode 100644 exercise-05-calculator/test_data/0000_in.txt create mode 100644 exercise-05-calculator/test_data/0000_out.txt create mode 100644 exercise-05-calculator/test_data/0000_out_win.txt create mode 100644 exercise-05-calculator/test_data/0001_in.txt create mode 100644 exercise-05-calculator/test_data/0001_out.txt create mode 100644 exercise-05-calculator/test_data/0001_out_win.txt create mode 100644 exercise-05-calculator/test_data/0002_in.txt create mode 100644 exercise-05-calculator/test_data/0002_out.txt create mode 100644 exercise-05-calculator/test_data/0002_out_win.txt create mode 100644 exercise-05-calculator/test_data/0003_in.txt create mode 100644 exercise-05-calculator/test_data/0003_out.txt create mode 100644 exercise-05-calculator/test_data/0003_out_win.txt create mode 100644 exercise-05-calculator/test_data/0004_in.txt create mode 100644 exercise-05-calculator/test_data/0004_out.txt create mode 100644 exercise-05-calculator/test_data/0004_out_win.txt create mode 100644 exercise-05-calculator/test_data/0005_in.txt create mode 100644 exercise-05-calculator/test_data/0005_out.txt create mode 100644 exercise-05-calculator/test_data/0005_out_win.txt diff --git a/exercise-05-calculator/test_data/0000_in.txt b/exercise-05-calculator/test_data/0000_in.txt new file mode 100644 index 0000000..d96b027 --- /dev/null +++ b/exercise-05-calculator/test_data/0000_in.txt @@ -0,0 +1 @@ +3.5 + 1.5 = diff --git a/exercise-05-calculator/test_data/0000_out.txt b/exercise-05-calculator/test_data/0000_out.txt new file mode 100644 index 0000000..4fc7051 --- /dev/null +++ b/exercise-05-calculator/test_data/0000_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +5 diff --git a/exercise-05-calculator/test_data/0000_out_win.txt b/exercise-05-calculator/test_data/0000_out_win.txt new file mode 100644 index 0000000..fe4108d --- /dev/null +++ b/exercise-05-calculator/test_data/0000_out_win.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +5 diff --git a/exercise-05-calculator/test_data/0001_in.txt b/exercise-05-calculator/test_data/0001_in.txt new file mode 100644 index 0000000..e751c2f --- /dev/null +++ b/exercise-05-calculator/test_data/0001_in.txt @@ -0,0 +1 @@ +3.5 / 1.5 = diff --git a/exercise-05-calculator/test_data/0001_out.txt b/exercise-05-calculator/test_data/0001_out.txt new file mode 100644 index 0000000..ea33cc6 --- /dev/null +++ b/exercise-05-calculator/test_data/0001_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +2 diff --git a/exercise-05-calculator/test_data/0001_out_win.txt b/exercise-05-calculator/test_data/0001_out_win.txt new file mode 100644 index 0000000..09a6fcd --- /dev/null +++ b/exercise-05-calculator/test_data/0001_out_win.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +2 diff --git a/exercise-05-calculator/test_data/0002_in.txt b/exercise-05-calculator/test_data/0002_in.txt new file mode 100644 index 0000000..8aa124a --- /dev/null +++ b/exercise-05-calculator/test_data/0002_in.txt @@ -0,0 +1 @@ +1000000 * 1.5e+8 = diff --git a/exercise-05-calculator/test_data/0002_out.txt b/exercise-05-calculator/test_data/0002_out.txt new file mode 100644 index 0000000..c34f246 --- /dev/null +++ b/exercise-05-calculator/test_data/0002_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +1.5e+14 diff --git a/exercise-05-calculator/test_data/0002_out_win.txt b/exercise-05-calculator/test_data/0002_out_win.txt new file mode 100644 index 0000000..1fd815f --- /dev/null +++ b/exercise-05-calculator/test_data/0002_out_win.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +1.5e+14 diff --git a/exercise-05-calculator/test_data/0003_in.txt b/exercise-05-calculator/test_data/0003_in.txt new file mode 100644 index 0000000..2ecb69a --- /dev/null +++ b/exercise-05-calculator/test_data/0003_in.txt @@ -0,0 +1 @@ +7 / -1 = diff --git a/exercise-05-calculator/test_data/0003_out.txt b/exercise-05-calculator/test_data/0003_out.txt new file mode 100644 index 0000000..63f6614 --- /dev/null +++ b/exercise-05-calculator/test_data/0003_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +-7 diff --git a/exercise-05-calculator/test_data/0003_out_win.txt b/exercise-05-calculator/test_data/0003_out_win.txt new file mode 100644 index 0000000..393ee41 --- /dev/null +++ b/exercise-05-calculator/test_data/0003_out_win.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +-7 diff --git a/exercise-05-calculator/test_data/0004_in.txt b/exercise-05-calculator/test_data/0004_in.txt new file mode 100644 index 0000000..31fbe44 --- /dev/null +++ b/exercise-05-calculator/test_data/0004_in.txt @@ -0,0 +1 @@ +abc / 1 = diff --git a/exercise-05-calculator/test_data/0004_out.txt b/exercise-05-calculator/test_data/0004_out.txt new file mode 100644 index 0000000..c64f981 --- /dev/null +++ b/exercise-05-calculator/test_data/0004_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +Nespravny vstup. diff --git a/exercise-05-calculator/test_data/0004_out_win.txt b/exercise-05-calculator/test_data/0004_out_win.txt new file mode 100644 index 0000000..da95d66 --- /dev/null +++ b/exercise-05-calculator/test_data/0004_out_win.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +Nespravny vstup. diff --git a/exercise-05-calculator/test_data/0005_in.txt b/exercise-05-calculator/test_data/0005_in.txt new file mode 100644 index 0000000..4d491c4 --- /dev/null +++ b/exercise-05-calculator/test_data/0005_in.txt @@ -0,0 +1 @@ +4 & 10 = diff --git a/exercise-05-calculator/test_data/0005_out.txt b/exercise-05-calculator/test_data/0005_out.txt new file mode 100644 index 0000000..c64f981 --- /dev/null +++ b/exercise-05-calculator/test_data/0005_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +Nespravny vstup. diff --git a/exercise-05-calculator/test_data/0005_out_win.txt b/exercise-05-calculator/test_data/0005_out_win.txt new file mode 100644 index 0000000..da95d66 --- /dev/null +++ b/exercise-05-calculator/test_data/0005_out_win.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +Nespravny vstup. From 81e9bfc58a384210349d714c847f95bc0b0b5b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Mon, 20 Oct 2025 17:37:51 +0200 Subject: [PATCH 05/12] ex-05: Initial solution --- exercise-05-calculator/main.c | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 exercise-05-calculator/main.c diff --git a/exercise-05-calculator/main.c b/exercise-05-calculator/main.c new file mode 100644 index 0000000..d2552bd --- /dev/null +++ b/exercise-05-calculator/main.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +int main() { + printf("Zadejte vzorec:\n"); + + double num1, num2; + char op; + if (scanf(" %lf %c %lf = ", &num1, &op, &num2) != 3) { + printf("Nespravny vstup.\n"); + return EXIT_FAILURE; + } + + if (op != '+' && op != '-' && op != '*' && op != '/') { + printf("Nespravny vstup.\n"); + return EXIT_FAILURE; + } + + if (op == '/' && num2 == 0) { + printf("Nespravny vstup.\n"); + return EXIT_FAILURE; + } + + double result = 0; + switch (op) { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = floor(num1 / num2); + break; + }; + + if (result > INT_MAX) { + printf("%.1e\n", result); + } else { + printf("%ld\n", (long)result); + } +} From 9cab9ed2e6ea077af9896241647ba9271865e177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Mon, 20 Oct 2025 17:38:28 +0200 Subject: [PATCH 06/12] ex-05: Additional test data --- exercise-05-calculator/test_data/0006_in.txt | 1 + exercise-05-calculator/test_data/0006_out.txt | 2 ++ exercise-05-calculator/test_data/0007_in.txt | 1 + exercise-05-calculator/test_data/0007_out.txt | 2 ++ exercise-05-calculator/test_data/0008_in.txt | 1 + exercise-05-calculator/test_data/0008_out.txt | 2 ++ exercise-05-calculator/test_data/0009_in.txt | 1 + exercise-05-calculator/test_data/0009_out.txt | 2 ++ 8 files changed, 12 insertions(+) create mode 100644 exercise-05-calculator/test_data/0006_in.txt create mode 100644 exercise-05-calculator/test_data/0006_out.txt create mode 100644 exercise-05-calculator/test_data/0007_in.txt create mode 100644 exercise-05-calculator/test_data/0007_out.txt create mode 100644 exercise-05-calculator/test_data/0008_in.txt create mode 100644 exercise-05-calculator/test_data/0008_out.txt create mode 100644 exercise-05-calculator/test_data/0009_in.txt create mode 100644 exercise-05-calculator/test_data/0009_out.txt diff --git a/exercise-05-calculator/test_data/0006_in.txt b/exercise-05-calculator/test_data/0006_in.txt new file mode 100644 index 0000000..77b865c --- /dev/null +++ b/exercise-05-calculator/test_data/0006_in.txt @@ -0,0 +1 @@ +0.848905 * 0.783596 = diff --git a/exercise-05-calculator/test_data/0006_out.txt b/exercise-05-calculator/test_data/0006_out.txt new file mode 100644 index 0000000..bcc338f --- /dev/null +++ b/exercise-05-calculator/test_data/0006_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +0.66519856238 diff --git a/exercise-05-calculator/test_data/0007_in.txt b/exercise-05-calculator/test_data/0007_in.txt new file mode 100644 index 0000000..7798ac2 --- /dev/null +++ b/exercise-05-calculator/test_data/0007_in.txt @@ -0,0 +1 @@ +-3.5 / 1.5 = diff --git a/exercise-05-calculator/test_data/0007_out.txt b/exercise-05-calculator/test_data/0007_out.txt new file mode 100644 index 0000000..8b9d863 --- /dev/null +++ b/exercise-05-calculator/test_data/0007_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +-2 diff --git a/exercise-05-calculator/test_data/0008_in.txt b/exercise-05-calculator/test_data/0008_in.txt new file mode 100644 index 0000000..8ea5482 --- /dev/null +++ b/exercise-05-calculator/test_data/0008_in.txt @@ -0,0 +1 @@ +0.984813 / 0.360992 = diff --git a/exercise-05-calculator/test_data/0008_out.txt b/exercise-05-calculator/test_data/0008_out.txt new file mode 100644 index 0000000..ea33cc6 --- /dev/null +++ b/exercise-05-calculator/test_data/0008_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +2 diff --git a/exercise-05-calculator/test_data/0009_in.txt b/exercise-05-calculator/test_data/0009_in.txt new file mode 100644 index 0000000..53cfeff --- /dev/null +++ b/exercise-05-calculator/test_data/0009_in.txt @@ -0,0 +1 @@ +2 * 3 diff --git a/exercise-05-calculator/test_data/0009_out.txt b/exercise-05-calculator/test_data/0009_out.txt new file mode 100644 index 0000000..c64f981 --- /dev/null +++ b/exercise-05-calculator/test_data/0009_out.txt @@ -0,0 +1,2 @@ +Zadejte vzorec: +Nespravny vstup. From 9e81c351e38758a1c7c17e1d9d6ba0dba636b86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Mon, 20 Oct 2025 21:05:54 +0200 Subject: [PATCH 07/12] ex-05: Improve edge case handling --- exercise-05-calculator/main.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/exercise-05-calculator/main.c b/exercise-05-calculator/main.c index d2552bd..f09f045 100644 --- a/exercise-05-calculator/main.c +++ b/exercise-05-calculator/main.c @@ -8,7 +8,8 @@ int main() { double num1, num2; char op; - if (scanf(" %lf %c %lf = ", &num1, &op, &num2) != 3) { + char equal_sign; + if (scanf("%lf %c %lf %c", &num1, &op, &num2, &equal_sign) != 4 || equal_sign != '=') { printf("Nespravny vstup.\n"); return EXIT_FAILURE; } @@ -35,13 +36,18 @@ int main() { result = num1 * num2; break; case '/': - result = floor(num1 / num2); + result = num1 / num2; + if (result < 0) { + result = ceil(num1 / num2); + } else { + result = floor(num1 / num2); + } break; - }; + } - if (result > INT_MAX) { - printf("%.1e\n", result); - } else { + if (fabs(result - (int)result) < 1e-9) { printf("%ld\n", (long)result); + } else { + printf("%.12g\n", result); } } From 1156dd9fd7a559030b2a9a58e1f32e27d1092754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Tue, 21 Oct 2025 09:18:50 +0200 Subject: [PATCH 08/12] test: Missing declaration in test script --- test/main.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/main.sh b/test/main.sh index 8c7d234..e5d90b1 100755 --- a/test/main.sh +++ b/test/main.sh @@ -9,6 +9,8 @@ RESET="\e[0m" DIFF_COMMAND="diff --color" +check_all=false + for arg in "$@"; do case "$arg" in --all|-a) From bbe5440be038ff1a0247ef5ccd06ce80fcb090db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Tue, 21 Oct 2025 16:36:30 +0200 Subject: [PATCH 09/12] hw-02: Additional test data --- homework-02-rail-building/test_data/0001_out.txt | 8 ++++---- homework-02-rail-building/test_data/0010_in.txt | 2 ++ homework-02-rail-building/test_data/0010_out.txt | 3 +++ homework-02-rail-building/test_data/0011_in.txt | 2 ++ homework-02-rail-building/test_data/0011_out.txt | 3 +++ homework-02-rail-building/test_data/0012_in.txt | 2 ++ homework-02-rail-building/test_data/0012_out.txt | 2 ++ homework-02-rail-building/test_data/0013_in.txt | 2 ++ homework-02-rail-building/test_data/0013_out.txt | 2 ++ homework-02-rail-building/test_data/0014_in.txt | 2 ++ homework-02-rail-building/test_data/0014_out.txt | 3 +++ homework-02-rail-building/test_data/0015_in.txt | 2 ++ homework-02-rail-building/test_data/0015_out.txt | 3 +++ 13 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 homework-02-rail-building/test_data/0010_in.txt create mode 100644 homework-02-rail-building/test_data/0010_out.txt create mode 100644 homework-02-rail-building/test_data/0011_in.txt create mode 100644 homework-02-rail-building/test_data/0011_out.txt create mode 100644 homework-02-rail-building/test_data/0012_in.txt create mode 100644 homework-02-rail-building/test_data/0012_out.txt create mode 100644 homework-02-rail-building/test_data/0013_in.txt create mode 100644 homework-02-rail-building/test_data/0013_out.txt create mode 100644 homework-02-rail-building/test_data/0014_in.txt create mode 100644 homework-02-rail-building/test_data/0014_out.txt create mode 100644 homework-02-rail-building/test_data/0015_in.txt create mode 100644 homework-02-rail-building/test_data/0015_out.txt diff --git a/homework-02-rail-building/test_data/0001_out.txt b/homework-02-rail-building/test_data/0001_out.txt index df01575..a05a8c6 100644 --- a/homework-02-rail-building/test_data/0001_out.txt +++ b/homework-02-rail-building/test_data/0001_out.txt @@ -1,9 +1,9 @@ Delky koleji: Vzdalenost: -= 10 * 32 + 13 * 33 -= 10 * 19 + 13 * 43 = 10 * 6 + 13 * 53 -= 10 * 71 + 13 * 3 -= 10 * 58 + 13 * 13 += 10 * 19 + 13 * 43 += 10 * 32 + 13 * 33 = 10 * 45 + 13 * 23 += 10 * 58 + 13 * 13 += 10 * 71 + 13 * 3 Celkem variant: 6 diff --git a/homework-02-rail-building/test_data/0010_in.txt b/homework-02-rail-building/test_data/0010_in.txt new file mode 100644 index 0000000..be73932 --- /dev/null +++ b/homework-02-rail-building/test_data/0010_in.txt @@ -0,0 +1,2 @@ +21 27 +- 567 diff --git a/homework-02-rail-building/test_data/0010_out.txt b/homework-02-rail-building/test_data/0010_out.txt new file mode 100644 index 0000000..f87ad5e --- /dev/null +++ b/homework-02-rail-building/test_data/0010_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Celkem variant: 4 diff --git a/homework-02-rail-building/test_data/0011_in.txt b/homework-02-rail-building/test_data/0011_in.txt new file mode 100644 index 0000000..8310498 --- /dev/null +++ b/homework-02-rail-building/test_data/0011_in.txt @@ -0,0 +1,2 @@ +0 13 ++ 25 diff --git a/homework-02-rail-building/test_data/0011_out.txt b/homework-02-rail-building/test_data/0011_out.txt new file mode 100644 index 0000000..5f7fc66 --- /dev/null +++ b/homework-02-rail-building/test_data/0011_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0012_in.txt b/homework-02-rail-building/test_data/0012_in.txt new file mode 100644 index 0000000..270a4da --- /dev/null +++ b/homework-02-rail-building/test_data/0012_in.txt @@ -0,0 +1,2 @@ +121 121 +- 3878 diff --git a/homework-02-rail-building/test_data/0012_out.txt b/homework-02-rail-building/test_data/0012_out.txt new file mode 100644 index 0000000..f48d3de --- /dev/null +++ b/homework-02-rail-building/test_data/0012_out.txt @@ -0,0 +1,2 @@ +Delky koleji: +Nespravny vstup. diff --git a/homework-02-rail-building/test_data/0013_in.txt b/homework-02-rail-building/test_data/0013_in.txt new file mode 100644 index 0000000..594d6a5 --- /dev/null +++ b/homework-02-rail-building/test_data/0013_in.txt @@ -0,0 +1,2 @@ +0 0 +- 150 diff --git a/homework-02-rail-building/test_data/0013_out.txt b/homework-02-rail-building/test_data/0013_out.txt new file mode 100644 index 0000000..f48d3de --- /dev/null +++ b/homework-02-rail-building/test_data/0013_out.txt @@ -0,0 +1,2 @@ +Delky koleji: +Nespravny vstup. diff --git a/homework-02-rail-building/test_data/0014_in.txt b/homework-02-rail-building/test_data/0014_in.txt new file mode 100644 index 0000000..27a314d --- /dev/null +++ b/homework-02-rail-building/test_data/0014_in.txt @@ -0,0 +1,2 @@ +13 0 ++ 25 diff --git a/homework-02-rail-building/test_data/0014_out.txt b/homework-02-rail-building/test_data/0014_out.txt new file mode 100644 index 0000000..5f7fc66 --- /dev/null +++ b/homework-02-rail-building/test_data/0014_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0015_in.txt b/homework-02-rail-building/test_data/0015_in.txt new file mode 100644 index 0000000..fadc038 --- /dev/null +++ b/homework-02-rail-building/test_data/0015_in.txt @@ -0,0 +1,2 @@ +13 15 ++ -25 diff --git a/homework-02-rail-building/test_data/0015_out.txt b/homework-02-rail-building/test_data/0015_out.txt new file mode 100644 index 0000000..529756d --- /dev/null +++ b/homework-02-rail-building/test_data/0015_out.txt @@ -0,0 +1,3 @@ +Delky koleji: +Vzdalenost: +Nespravny vstup. From 21dee66e99a1588f1ce271b3bef3a1d629721b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Tue, 21 Oct 2025 16:36:50 +0200 Subject: [PATCH 10/12] hw-02: First implementation --- homework-02-rail-building/main.c | 196 +++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 homework-02-rail-building/main.c diff --git a/homework-02-rail-building/main.c b/homework-02-rail-building/main.c new file mode 100644 index 0000000..bae721f --- /dev/null +++ b/homework-02-rail-building/main.c @@ -0,0 +1,196 @@ +#include +#include +#include + +#ifdef __PROGTEST__ +#define debug(...) ((void)0) +#else +#define debug(fmt, ...) \ + fprintf(stdout, "[DEBUG %s:%d %s()] " fmt "\n", __FILE__, __LINE__, \ + __func__, ##__VA_ARGS__) +#endif + +typedef long long int lli; + +typedef struct rails_t { + lli rail_length_1; + lli *larger_length; + lli rail_length_2; + lli *shorter_length; + lli total_distance; +} rails_t; + +// find the pivot using the smaller of the rail lengths +// once pivot is found, the smallest step that will be used is +// gcd(rail length 1, rail length 2) +// + +int invalid_input() { + printf("Nespravny vstup.\n"); + return EXIT_FAILURE; +} + +// https://www.geeksforgeeks.org/dsa/steins-algorithm-for-finding-gcd/ +int gcd(int a, int b) { + if (a == 0) + return b; + if (b == 0) + return a; + + int k; + for (k = 0; ((a | b) & 1) == 0; ++k) { + a >>= 1; + b >>= 1; + } + + while ((a & 1) == 0) + a >>= 1; + + do { + while ((b & 1) == 0) + b >>= 1; + + if (a > b) { + a ^= b; + b ^= a; + a ^= b; + } + + b = (b - a); + } while (b != 0); + + return a << k; +} + +lli find_pivot(rails_t rails) { + lli pivot = -1; + + lli current_step; + + lli ratio = (rails.total_distance / *rails.shorter_length); + + for (lli i = 1; i < ratio; i++) { + current_step = rails.total_distance - i * (*rails.shorter_length); + + if (current_step % (*rails.larger_length) == 0) { + debug("Found pivot %lld\n", i); + pivot = i; + break; + } + } + + return pivot; +} + +lli find_smallest_step(rails_t rails) { + return *rails.larger_length / gcd(rails.rail_length_1, rails.rail_length_2); +} + +lli find_solutions(rails_t rails, bool verbose) { + lli variants = 0; + + lli pivot = find_pivot(rails); + lli smallest_step = find_smallest_step(rails); + debug("Smallest step = %lld", smallest_step); + + if (pivot != -1) { + lli i; + for (i = pivot;; i += smallest_step) { + + if ((rails.total_distance - i * (*rails.shorter_length)) < 0) { + break; + } + + if (verbose) { + + if (*rails.shorter_length == rails.rail_length_1) { + printf("= %lld * %lld + %lld * %lld\n", rails.rail_length_1, i, + rails.rail_length_2, + (rails.total_distance - i * rails.rail_length_1) / + rails.rail_length_2); + } else { + printf("= %lld * %lld + %lld * %lld\n", rails.rail_length_1, + (rails.total_distance - i * rails.rail_length_2) / + rails.rail_length_1, + rails.rail_length_2, i); + } + } + + variants++; + } + } + + return variants; +} + +int main() { + printf("Delky koleji:\n"); + + lli rail_length_1 = -1, rail_length_2 = -1; + if (scanf(" %lld %lld", &rail_length_1, &rail_length_2) != 2 || + (rail_length_1 <= 0 && rail_length_2 <= 0) || + (rail_length_1 == rail_length_2)) { + return invalid_input(); + } + + printf("Vzdalenost:\n"); + + lli total_distance = 0; + char operand; + + if (scanf(" %c %lld", &operand, &total_distance) != 2 || + (operand != '+' && operand != '-')) { + return invalid_input(); + } + + lli variants = 0; + bool verbose = operand == '+'; + if (total_distance < 0) { + return invalid_input(); + } + if (total_distance == 0) { + if (verbose) { + printf("= %lld * 0 + %lld * 0\n", rail_length_1, rail_length_2); + } + variants++; + } else if (rail_length_1 == 0 && total_distance % rail_length_2 == 0) { + if (verbose) { + printf("= %lld * %lld + %lld * 0\n", rail_length_1, + total_distance / rail_length_2, rail_length_2); + } + } else if (rail_length_2 == 0 && total_distance % rail_length_1 == 0) { + if (verbose) { + printf("= %lld * 0 + %lld * %lld\n", rail_length_1, rail_length_2, + total_distance / rail_length_1); + } + } else if (rail_length_1 > 0 && rail_length_2 > 0) { + + lli *smaller = + rail_length_1 > rail_length_2 ? &rail_length_2 : &rail_length_1; + lli *larger = + rail_length_1 > rail_length_2 ? &rail_length_1 : &rail_length_2; + + rails_t rails; + rails.rail_length_1 = rail_length_1; + rails.rail_length_2 = rail_length_2; + rails.total_distance = total_distance; + rails.shorter_length = smaller; + rails.larger_length = larger; + + if (total_distance % rail_length_1 == 0) { + if (verbose) { + printf("= %lld * %lld + %lld * 0\n", rail_length_1, + total_distance / rail_length_1, rail_length_2); + } + variants++; + } + + variants += find_solutions(rails, verbose); + } + + if (variants != 0) { + printf("Celkem variant: %lld\n", variants); + } else { + printf("Reseni neexistuje.\n"); + } +} From a3b40590a6f51cec25df84880f302134e20bac64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Wed, 22 Oct 2025 08:44:04 +0200 Subject: [PATCH 11/12] hw-02: Additional test data --- homework-02-rail-building/test_data/0011_in.txt | 2 -- homework-02-rail-building/test_data/0011_out.txt | 3 --- homework-02-rail-building/test_data/0014_in.txt | 2 -- homework-02-rail-building/test_data/0014_out.txt | 3 --- homework-02-rail-building/test_data/0017_in.txt | 2 ++ homework-02-rail-building/test_data/0017_out.txt | 2 ++ 6 files changed, 4 insertions(+), 10 deletions(-) delete mode 100644 homework-02-rail-building/test_data/0011_in.txt delete mode 100644 homework-02-rail-building/test_data/0011_out.txt delete mode 100644 homework-02-rail-building/test_data/0014_in.txt delete mode 100644 homework-02-rail-building/test_data/0014_out.txt create mode 100644 homework-02-rail-building/test_data/0017_in.txt create mode 100644 homework-02-rail-building/test_data/0017_out.txt diff --git a/homework-02-rail-building/test_data/0011_in.txt b/homework-02-rail-building/test_data/0011_in.txt deleted file mode 100644 index 8310498..0000000 --- a/homework-02-rail-building/test_data/0011_in.txt +++ /dev/null @@ -1,2 +0,0 @@ -0 13 -+ 25 diff --git a/homework-02-rail-building/test_data/0011_out.txt b/homework-02-rail-building/test_data/0011_out.txt deleted file mode 100644 index 5f7fc66..0000000 --- a/homework-02-rail-building/test_data/0011_out.txt +++ /dev/null @@ -1,3 +0,0 @@ -Delky koleji: -Vzdalenost: -Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0014_in.txt b/homework-02-rail-building/test_data/0014_in.txt deleted file mode 100644 index 27a314d..0000000 --- a/homework-02-rail-building/test_data/0014_in.txt +++ /dev/null @@ -1,2 +0,0 @@ -13 0 -+ 25 diff --git a/homework-02-rail-building/test_data/0014_out.txt b/homework-02-rail-building/test_data/0014_out.txt deleted file mode 100644 index 5f7fc66..0000000 --- a/homework-02-rail-building/test_data/0014_out.txt +++ /dev/null @@ -1,3 +0,0 @@ -Delky koleji: -Vzdalenost: -Reseni neexistuje. diff --git a/homework-02-rail-building/test_data/0017_in.txt b/homework-02-rail-building/test_data/0017_in.txt new file mode 100644 index 0000000..4abca5a --- /dev/null +++ b/homework-02-rail-building/test_data/0017_in.txt @@ -0,0 +1,2 @@ +4014 0 +- 2227 diff --git a/homework-02-rail-building/test_data/0017_out.txt b/homework-02-rail-building/test_data/0017_out.txt new file mode 100644 index 0000000..f48d3de --- /dev/null +++ b/homework-02-rail-building/test_data/0017_out.txt @@ -0,0 +1,2 @@ +Delky koleji: +Nespravny vstup. From 3565364e12a2f8c4d0a6373bf6a1b81f3579890a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Wed, 22 Oct 2025 08:44:14 +0200 Subject: [PATCH 12/12] hw-02: Final implementation --- homework-02-rail-building/main.c | 197 +++++++++++++------------------ 1 file changed, 80 insertions(+), 117 deletions(-) diff --git a/homework-02-rail-building/main.c b/homework-02-rail-building/main.c index bae721f..a848a66 100644 --- a/homework-02-rail-building/main.c +++ b/homework-02-rail-building/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,117 +7,107 @@ #define debug(...) ((void)0) #else #define debug(fmt, ...) \ - fprintf(stdout, "[DEBUG %s:%d %s()] " fmt "\n", __FILE__, __LINE__, \ + printf("[DEBUG %s:%d %s()] " fmt "\n", __FILE__, __LINE__, \ __func__, ##__VA_ARGS__) #endif typedef long long int lli; +typedef long double ld; typedef struct rails_t { - lli rail_length_1; - lli *larger_length; - lli rail_length_2; - lli *shorter_length; - lli total_distance; + lli r1; + lli r2; + lli d; } rails_t; -// find the pivot using the smaller of the rail lengths -// once pivot is found, the smallest step that will be used is -// gcd(rail length 1, rail length 2) -// +typedef struct eea_gcd_t { + lli g; + lli x; + lli y; +} eea_gcd_t; int invalid_input() { printf("Nespravny vstup.\n"); return EXIT_FAILURE; } -// https://www.geeksforgeeks.org/dsa/steins-algorithm-for-finding-gcd/ -int gcd(int a, int b) { - if (a == 0) - return b; - if (b == 0) - return a; - - int k; - for (k = 0; ((a | b) & 1) == 0; ++k) { - a >>= 1; - b >>= 1; +eea_gcd_t extended_euclidian_gcd(lli a, lli b) { + if (a == 0) { + eea_gcd_t res; + res.g = b; + res.x = 0; + res.y = 1; + return res; } - while ((a & 1) == 0) - a >>= 1; + eea_gcd_t res = extended_euclidian_gcd(b % a, a); - do { - while ((b & 1) == 0) - b >>= 1; + lli x = res.y - (b / a) * res.x; + lli y = res.x; - if (a > b) { - a ^= b; - b ^= a; - a ^= b; - } + res.x = x; + res.y = y; - b = (b - a); - } while (b != 0); - - return a << k; + return res; } -lli find_pivot(rails_t rails) { - lli pivot = -1; - - lli current_step; - - lli ratio = (rails.total_distance / *rails.shorter_length); - - for (lli i = 1; i < ratio; i++) { - current_step = rails.total_distance - i * (*rails.shorter_length); - - if (current_step % (*rails.larger_length) == 0) { - debug("Found pivot %lld\n", i); - pivot = i; - break; - } - } - - return pivot; -} - -lli find_smallest_step(rails_t rails) { - return *rails.larger_length / gcd(rails.rail_length_1, rails.rail_length_2); -} lli find_solutions(rails_t rails, bool verbose) { lli variants = 0; - lli pivot = find_pivot(rails); - lli smallest_step = find_smallest_step(rails); - debug("Smallest step = %lld", smallest_step); + if (rails.d == 0) { + debug("Zero total distance"); + if (verbose) { + printf("= %lld * 0 + %lld * 0\n", rails.r1, rails.r2); + } + return 1; + } - if (pivot != -1) { - lli i; - for (i = pivot;; i += smallest_step) { + if (rails.r1 == 0 && rails.d % rails.r2 == 0) { + if (verbose) { + printf("= %lld * %lld + %lld * 0\n", rails.r1, rails.d / rails.r2, + rails.r2); + } + return 1; + } - if ((rails.total_distance - i * (*rails.shorter_length)) < 0) { - break; - } + if (rails.r2 == 0 && rails.d % rails.r1 == 0) { + if (verbose) { + printf("= %lld * 0 + %lld * %lld\n", rails.r1, rails.r2, + rails.d / rails.r1); + } + return 1; + } - if (verbose) { + eea_gcd_t eea = extended_euclidian_gcd(rails.r1, rails.r2); + lli g = eea.g; - if (*rails.shorter_length == rails.rail_length_1) { - printf("= %lld * %lld + %lld * %lld\n", rails.rail_length_1, i, - rails.rail_length_2, - (rails.total_distance - i * rails.rail_length_1) / - rails.rail_length_2); - } else { - printf("= %lld * %lld + %lld * %lld\n", rails.rail_length_1, - (rails.total_distance - i * rails.rail_length_2) / - rails.rail_length_1, - rails.rail_length_2, i); - } - } + if (rails.d % g != 0) { + return 0; + } - variants++; + ld d_g = (ld)rails.d / (ld)g; + ld x0 = (ld)eea.x * d_g; + ld y0 = (ld)eea.y * d_g; + + + lli a_g = rails.r1 / g; + lli b_g = rails.r2 / g; + + lli n_min = (lli)ceil(-x0 / (ld)b_g); + lli n_max = (lli)floor(y0 / (ld)a_g); + + if (n_max < n_min) { + return 0; + } + + variants += n_max - n_min + 1; + + if (verbose) { + for (lli n = n_min; n <= n_max; n++) { + lli x = x0 + n * b_g; + lli y = y0 - n * a_g; + printf("= %lld * %lld + %lld * %lld\n", rails.r1, x, rails.r2, y); } } @@ -128,7 +119,7 @@ int main() { lli rail_length_1 = -1, rail_length_2 = -1; if (scanf(" %lld %lld", &rail_length_1, &rail_length_2) != 2 || - (rail_length_1 <= 0 && rail_length_2 <= 0) || + (rail_length_1 <= 0 || rail_length_2 <= 0) || (rail_length_1 == rail_length_2)) { return invalid_input(); } @@ -143,47 +134,19 @@ int main() { return invalid_input(); } - lli variants = 0; - bool verbose = operand == '+'; if (total_distance < 0) { return invalid_input(); } - if (total_distance == 0) { - if (verbose) { - printf("= %lld * 0 + %lld * 0\n", rail_length_1, rail_length_2); - } - variants++; - } else if (rail_length_1 == 0 && total_distance % rail_length_2 == 0) { - if (verbose) { - printf("= %lld * %lld + %lld * 0\n", rail_length_1, - total_distance / rail_length_2, rail_length_2); - } - } else if (rail_length_2 == 0 && total_distance % rail_length_1 == 0) { - if (verbose) { - printf("= %lld * 0 + %lld * %lld\n", rail_length_1, rail_length_2, - total_distance / rail_length_1); - } - } else if (rail_length_1 > 0 && rail_length_2 > 0) { - lli *smaller = - rail_length_1 > rail_length_2 ? &rail_length_2 : &rail_length_1; - lli *larger = - rail_length_1 > rail_length_2 ? &rail_length_1 : &rail_length_2; + lli variants = 0; + bool verbose = operand == '+'; + + if (rail_length_1 > 0 && rail_length_2 > 0) { rails_t rails; - rails.rail_length_1 = rail_length_1; - rails.rail_length_2 = rail_length_2; - rails.total_distance = total_distance; - rails.shorter_length = smaller; - rails.larger_length = larger; - - if (total_distance % rail_length_1 == 0) { - if (verbose) { - printf("= %lld * %lld + %lld * 0\n", rail_length_1, - total_distance / rail_length_1, rail_length_2); - } - variants++; - } + rails.r1 = rail_length_1; + rails.r2 = rail_length_2; + rails.d = total_distance; variants += find_solutions(rails, verbose); }