Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 12 / fibb.cpp
diff options
context:
space:
mode:
Commitbd775b43bdf729aa151fe851d1bed9aef52622a1 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-01-05 18:18:04 +0200
Committer
 GitHub <noreply@github.com>
2022-01-05 18:18:04 +0200
Files0437dc13e3f4e8feb8ffddbea9db9b610d65a7b0 /Sem. 12/fibb.cpp
Introduction_to_programming_FMI-bd775b43bdf729aa151fe851d1bed9aef52622a1.tar
Introduction_to_programming_FMI-bd775b43bdf729aa151fe851d1bed9aef52622a1.tar.gz
Introduction_to_programming_FMI-bd775b43bdf729aa151fe851d1bed9aef52622a1.zip
Parent0744c3190c20f0af9b0d2bd9c78d2e80c4585e27 (diff)
Create fibb.cpp
1 files changed, 28 insertions, 0 deletions
-rw-r--r--Sem. 12/fibb.cpp28
diff --git a/Sem. 12/fibb.cpp b/Sem. 12/fibb.cpp
new file mode 100644
index 0000000..33eb899
--- /dev/null
+++ b/Sem. 12/fibb.cpp
@@ -0,0 +1,28 @@
+ #include <iostream>
+
+unsigned fibbRec(unsigned n, int* cache)
+{
+ if (n <= 1)
+ return 1;
+ if (cache[n] != 0) //вече е сметнато
+ return cache[n];
+
+ unsigned result = fibbRec(n - 1, cache) + fibbRec(n - 2, cache);
+ cache[n] = result;
+ return result;
+}
+unsigned fibb(unsigned n)
+{
+ int* cache = new int[n + 1];
+ for (unsigned i = 0; i < n + 1; i++)
+ cache[i] = 0;
+
+ unsigned result = fibbRec(n, cache);
+ delete[] cache;
+ return result;
+}
+
+int main()
+{
+ std::cout << fibb(40) << std::endl;
+}