本题目来源为SICP 习题1.19
先来看常规的斐波那契计算方法
int fib(int n)
{
return n<2 ? n:fib(n-1)+fib(n-2);
}
不难发现,这种计算方法做了太多的冗余计算,实际上这一过程的增长相对于n是指数的,我们可以通过建立备忘录的方式来将算法复杂度减成线性的
#include <vector>
using namespace std;
vector <int> opt; //opt在main函数中初始化大小为n
int fib(int n)
{