Advertisement
Atheuz

Memoized recursive fibonnaci

Jan 25th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <map>
  4.  
  5. __int64 fib(int n, std::map<int, __int64> &memo) {
  6.     if (!memo.count(n)) {
  7.         memo[n] = fib(n-1, memo) + fib(n-2, memo);
  8.     }
  9.     return memo.at(n);
  10. }
  11.  
  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.     std::map<int, __int64> memo;
  15.     memo[0] = 0;
  16.     memo[1] = 1;
  17.  
  18.     for(int i=0; i < 100; i++) {
  19.         std::cout << fib(i, memo) << " ";
  20.     }
  21.  
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement