fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //Memoization
  5. //Top-down because calculating 0, 1, and reaching till bottom N
  6.  
  7. int memo[100];
  8.  
  9. int f(int n)
  10. {
  11. if(n == 0) return 0;
  12. if(n == 1) return 1;
  13.  
  14. if(memo[n]) return memo[n];
  15. return memo[n] = f(n-1) + f(n-2);
  16. }
  17.  
  18. int main (void)
  19. {
  20. int n;
  21. cin >> n;
  22. cout << f(n);
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5476KB
stdin
10
stdout
55