Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #include <set>
- using namespace std;
- /*
- da se napise programa koja ke gi sobere cifrite na eden broj
- rekurzivno
- **/
- int dp[1005];
- int fib(int n) {
- if(n <= 2) {
- return 1;
- }
- if(dp[n] != -1) {
- return dp[n];
- }
- return dp[n] = fib(n - 1) + fib(n - 2);
- }
- int main() {
- ios::sync_with_stdio(false);
- memset(dp, -1, sizeof dp);
- cout << fib(5) << endl;
- return 0;
- }
- // fib(5) = fib(4) + [fib(3)] = 3 + 2 = 5
- // fib(4) = fib(3) + fib(2) = 2 + 1 = 3
- // fib(3) = fib(2) + fib(1) = 2
- // fib(2) = 1
- // fib(1) = 1
- // fib(2) = 1
- // fib(3) = 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement