aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--week10/ex1.cpp13
-rw-r--r--week10/ex2.cpp12
-rw-r--r--week10/ex3.cpp27
-rw-r--r--week10/ex4.cpp23
-rw-r--r--week10/ex5.cpp44
5 files changed, 119 insertions, 0 deletions
diff --git a/week10/ex1.cpp b/week10/ex1.cpp
new file mode 100644
index 0000000..9467043
--- /dev/null
+++ b/week10/ex1.cpp
@@ -0,0 +1,13 @@
+#include <iostream>
+
+int gcd(int a, int b) {
+ if (a == b) return a;
+ if (a > b) return gcd(a-b, b);
+ return gcd(b, a);
+}
+
+int main() {
+ int a, b;
+ std::cin >> a >> b;
+ std::cout << gcd(a, b) << std::endl;
+}
diff --git a/week10/ex2.cpp b/week10/ex2.cpp
new file mode 100644
index 0000000..3d3e9a1
--- /dev/null
+++ b/week10/ex2.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/week10/ex3.cpp b/week10/ex3.cpp
new file mode 100644
index 0000000..1edf55d
--- /dev/null
+++ b/week10/ex3.cpp
@@ -0,0 +1,27 @@
+#include <iostream>
+
+void printCharNTimes(int n, char c) {
+ for (int i = 0; i < n; i++) {
+ std::cout << c;
+ }
+}
+
+void hourglass(int lines, int whitespaces, int nonwhitespace) {
+ if (lines == 0) return;
+
+ printCharNTimes(whitespaces, ' ');
+ printCharNTimes(nonwhitespace, '+');
+ std::cout << std::endl;
+
+ hourglass(lines - 1, whitespaces + 1, nonwhitespace - 2);
+
+ printCharNTimes(whitespaces, ' ');
+ printCharNTimes(nonwhitespace, '#');
+ std::cout << std::endl;
+}
+
+int main() {
+ int N;
+ std::cin >> N;
+ hourglass(N, 0, 2*N - 1);
+}
diff --git a/week10/ex4.cpp b/week10/ex4.cpp
new file mode 100644
index 0000000..ec05144
--- /dev/null
+++ b/week10/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/week10/ex5.cpp b/week10/ex5.cpp
new file mode 100644
index 0000000..895c729
--- /dev/null
+++ b/week10/ex5.cpp
@@ -0,0 +1,44 @@
+#include <iostream>
+#include <cstring>
+
+void skipWhitespaces(char*& str) {
+ while (*str == ' ') str++;
+}
+
+int calculateExpression(char*& expr) {
+ if (*expr == '\0') return 0;
+
+ skipWhitespaces(expr);
+
+ // Ако започваме с цифра, връщаме числото и го пропускаме в израза
+ if ('0' <= expr[0] && expr[0] <= '9') {
+ int ret = atoi(expr);
+ while ('0' <= *expr && *expr <= '9') expr++;
+ return ret;
+ }
+
+ expr++; // пропускаме (
+ int leftHandSide = calculateExpression(expr);
+ skipWhitespaces(expr);
+
+ char op = *expr;
+ expr++;
+
+ skipWhitespaces(expr);
+ int rightHandSide = calculateExpression(expr);
+
+ skipWhitespaces(expr);
+ expr++; // пропускаме )
+
+ if (op == '+') {
+ return leftHandSide + rightHandSide;
+ }
+ return leftHandSide * rightHandSide;
+}
+
+int main() {
+ char buf[1025];
+ std::cin.getline(buf, 1025);
+ char* ptrCopy = buf;
+ std::cout << calculateExpression(ptrCopy) << std::endl;
+}