一、递归算法
function recurFib(n) {
if (n < 2) {
return n;
}
else {
return recurFib(n-1) + recurFib(n-2);
}
}
alert(recurFib(10));//将显示55
二、动态规划法
function dynFib(n) {
var val = [];
for (var i = 0; i <= n; ++i) {
val[i] = 0;
}
if (n == 1 || n == 2) {
return 1;
}
else {
val[1] = 1;
val[2]
function recurFib(n) {
if (n < 2) {
return n;
}
else {
return recurFib(n-1) + recurFib(n-2);
}
}
alert(recurFib(10));//将显示55
![](http://www.108js.com/article/article5/img2/50095-2.gif)
![](http://www.108js.com/article/article5/img2/50095-3.gif)
![](http://www.108js.com/article/article5/img2/50095-4.gif)
二、动态规划法
function dynFib(n) {
var val = [];
for (var i = 0; i <= n; ++i) {
val[i] = 0;
}
if (n == 1 || n == 2) {
return 1;
}
else {
val[1] = 1;
val[2]