aboutsummaryrefslogtreecommitdiff
path: root/week11
diff options
context:
space:
mode:
Diffstat (limited to 'week11')
-rw-r--r--week11/ex1.cpp12
-rw-r--r--week11/ex2.cpp44
-rw-r--r--week11/ex3.cpp30
-rw-r--r--week11/ex4.cpp23
-rw-r--r--week11/ex5.cpp99
5 files changed, 208 insertions, 0 deletions
diff --git a/week11/ex1.cpp b/week11/ex1.cpp
new file mode 100644
index 0000000..3d3e9a1
--- /dev/null
+++ b/week11/ex1.cpp
@@ -0,0 +1,12 @@
+#include <iostream>
+
+int fibIndex(const int N, int count = 0, int a = 0, int b = 1) {
+ if (N == a) return count;
+ return fibIndex(N, count + 1, b, a+b);
+}
+
+int main() {
+ int N;
+ std::cin >> N;
+ std::cout << fibIndex(N) << std::endl;
+}
diff --git a/week11/ex2.cpp b/week11/ex2.cpp
new file mode 100644
index 0000000..b4cac7b
--- /dev/null
+++ b/week11/ex2.cpp
@@ -0,0 +1,44 @@
+#include <iostream>
+
+// а) подточка
+void printCharNTimes(int n, char c) {
+ for (int i = 0; i < n; i++) {
+ std::cout << c;
+ }
+ std::cout << std::endl;
+}
+
+void triangleA(const int N, int current = 1) {
+ if (current > N) return;
+
+ printCharNTimes(current, '+');
+
+ triangleA(N, current + 1);
+}
+
+// б) подточка
+void triangleB(const int N, int current = 1) {
+ if (current > N) return;
+
+ triangleA(N, current + 1);
+
+ printCharNTimes(current, '+');
+}
+
+
+// в) подточка
+void triangleC(const int N, int current = 1) {
+ if (current > N) return;
+
+ printCharNTimes(current, '+');
+
+ triangleC(N, current + 1);
+
+ printCharNTimes(current, '#');
+}
+
+int main() {
+ int N;
+ std::cin >> N;
+ triangleC(N);
+}
diff --git a/week11/ex3.cpp b/week11/ex3.cpp
new file mode 100644
index 0000000..bdcf937
--- /dev/null
+++ b/week11/ex3.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+
+void ages(const int mult, const int sum, int youngest, int middle, int oldest, bool &foundSolution) {
+ // Не проверяваме за сума, понеже винаги правим такива извиквания, че сумата да е коректна
+ if (youngest * middle * oldest == mult) {
+ std::cout << youngest << " " << middle << " " << oldest << std::endl;
+ foundSolution = true;
+ return;
+ }
+
+ // Правим middle по-стар като копенсираме чрез по-млад oldest
+ if (!foundSolution && oldest - 1 > middle + 1)
+ ages(mult, sum, youngest, middle + 1, oldest - 1, foundSolution);
+ // Правим youngest по-стар, като middle трябва да има най-малко същата му възраст
+ if (!foundSolution && sum - 2*(youngest + 1) > youngest + 1)
+ ages(mult, sum, youngest + 1, youngest + 1, sum - 2*(youngest + 1), foundSolution);
+}
+
+void ages(const int mult, const int sum) {
+ bool foundSolution = false;
+ // Започваме с минималните възможни възрасти за youngest и middle
+ ages(mult, sum, 1, 1, sum - 2, foundSolution);
+}
+
+int main() {
+ int mult, sum;
+ std::cin >> mult >> sum;
+
+ ages(mult, sum);
+}
diff --git a/week11/ex4.cpp b/week11/ex4.cpp
new file mode 100644
index 0000000..ec05144
--- /dev/null
+++ b/week11/ex4.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+
+void printNumbers(int currentBit, char* buf, const int N) {
+ if (currentBit == 0) {
+ std::cout << buf << std::endl;
+ return;
+ }
+ buf[N - currentBit] = '0';
+ printNumbers(currentBit - 1, buf, N);
+ buf[N - currentBit] = '1';
+ printNumbers(currentBit - 1, buf, N);
+}
+
+int main() {
+ int N;
+ std::cin >> N;
+
+ char* buf = new char[N+1];
+ buf[N] = '\0';
+
+ printNumbers(N, buf, N);
+ delete[] buf;
+}
diff --git a/week11/ex5.cpp b/week11/ex5.cpp
new file mode 100644
index 0000000..9b9cc84
--- /dev/null
+++ b/week11/ex5.cpp
@@ -0,0 +1,99 @@
+#include <iostream>
+
+int* firstEmpty(int board[4][4]) {
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ if (board[i][j] == -1)
+ return &board[i][j];
+ }
+ }
+ return nullptr;
+}
+
+bool isSolvedTakuzo(int board[4][4]) {
+ // Бройка в ред или колона
+ // Не е нужно да пазим или проверяваме броя 1ци, понеже той е 4 - zeroes
+ int zeroes = 0;
+ for (int i = 0; i < 4; i++) {
+ if (board[i][0] == 0) zeroes++;
+ }
+
+ // Бройка на редове
+ for (int i = 0; i < 4; i++) {
+ int currentZeroes = 0;
+ for (int j = 0; j < 4; j++) {
+ if (board[i][j] == 0) currentZeroes++;
+ }
+ if (currentZeroes != zeroes) return false;
+ }
+
+ // Бройка на колони
+ for (int i = 0; i < 4; i++) {
+ int currentZeroes = 0;
+ for (int j = 0; j < 4; j++) {
+ if (board[j][i] == 0) currentZeroes++;
+ }
+ if (currentZeroes != zeroes) return false;
+ }
+
+ // Последователни в ред
+ for (int i = 0; i < 4; i++) {
+ for (int j = 2; j < 4; j++) {
+ if (board[i][j - 2] == board[i][j - 1] && board[i][j - 1] == board[i][j]) return false;
+ }
+ }
+
+ // Последователни в колона
+ for (int i = 2; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ if (board[i - 2][j] == board[i - 1][j] && board[i - 1][j] == board[i][j]) return false;
+ }
+ }
+
+ return true;
+}
+
+void printTakuzo(int board[4][4]) {
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ std::cout << board[i][j] << " ";
+ }
+ std::cout << std::endl;
+ }
+}
+
+void solveTakuzo(int board[4][4], bool &isSolved) {
+ int* toFill = firstEmpty(board);
+ if (toFill == nullptr) {
+ if (isSolvedTakuzo(board)) {
+ isSolved = true;
+ printTakuzo(board);
+ }
+ return;
+ }
+
+ if (!isSolved) {
+ *toFill = 0;
+ solveTakuzo(board, isSolved);
+ }
+ if (!isSolved) {
+ *toFill = 1;
+ solveTakuzo(board, isSolved);
+ }
+ *toFill = -1;
+}
+
+void solveTakuzo(int board[4][4]) {
+ bool isSolved = false;
+ solveTakuzo(board, isSolved);
+}
+
+int main() {
+ int board[4][4];
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ std::cin >> board[i][j];
+ }
+ }
+ solveTakuzo(board);
+}