记忆话搜索来实现斐波那契数列
所谓的记忆话搜索,这还不是用了数组来存储么!
#include <iostream> #include <cstdio> using namespace std; #define BUG puts("here"); const int N = 15; int fib[15]; void init() { fib[0] = 1; fib[1] = 1; for(int i = 2; i < N; i++) { fib[i] = -1; } } int get_fib(int n) { if(fib[n] != -1) return fib[n]; fib[n] = get_fib(n-1) + get_fib(n-2); return fib[n]; } int main() { init(); cout << get_fib(5) << endl; return 0; }