Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 15 / task1.cpp
diff options
context:
space:
mode:
Commit91ad02e1a054521b212b2d2f4fba691c56f8d073 (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2022-01-17 21:47:36 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2022-01-17 21:47:36 +0200
Files07db3fe4286dace1723b9d220497cbc36d0b4849 /Sem. 15/task1.cpp
Introduction_to_programming_FMI-91ad02e1a054521b212b2d2f4fba691c56f8d073.tar
Introduction_to_programming_FMI-91ad02e1a054521b212b2d2f4fba691c56f8d073.tar.gz
Introduction_to_programming_FMI-91ad02e1a054521b212b2d2f4fba691c56f8d073.zip
Parent7104c90385c02473bba9880b0fcd28ffbf1940bc (diff)
Parent2d3edb414454ad28554cf73ed8fabe86f17ff337 (diff)
Merge branch 'main' of https://github.com/Angeld55/Introduction_to_programming_FMI
1 files changed, 46 insertions, 0 deletions
-rw-r--r--Sem. 15/task1.cpp46
diff --git a/Sem. 15/task1.cpp b/Sem. 15/task1.cpp
new file mode 100644
index 0000000..502b7a5
--- /dev/null
+++ b/Sem. 15/task1.cpp
@@ -0,0 +1,46 @@
+#include <iostream>
+
+int min(int a, int b)
+{
+ return a < b ? a : b;
+}
+int min(int a, int b, int c)
+{
+ return min(min(a, b), c);
+}
+int getMinOperations(size_t N, int* cache)
+{
+ if (N == 1)
+ return 0;
+
+ if (cache[N] != 0)
+ return cache[N];
+
+ int dvBy3 = INT_MAX;
+ int dvBy2 = INT_MAX;
+ int s1 = INT_MAX;
+
+ if (N % 3 == 0)
+ dvBy3 = getMinOperations(N / 3, cache);
+ if (N % 2 == 0)
+ dvBy2 = getMinOperations(N / 2, cache);
+ s1 = getMinOperations(N - 1, cache);
+
+ int result = 1 + min(dvBy2, dvBy3, s1);
+ cache[N] = result;
+ return result;
+}
+
+int getMinOperationsWrapper(size_t N)
+{
+ int* cache = new int[N + 1];
+ for (size_t i = 0; i < N + 1; i++)
+ cache[i] = 0;
+
+ return getMinOperations(N, cache);
+}
+
+int main()
+{
+ std::cout << getMinOperationsWrapper(19);// 1 -> 3 -> 6 -> 18 -> 19
+} \ No newline at end of file