//使用递归去解决问题虽然简洁, 但效率不高,转为动态规划较好 function recurFib(n) {//斐波那契数列——递归 if (n <= 2) { return 1; } else { return recurFib(n - 1) + recurFib(n - 2); } } function dynFib(n) {//斐波那契数列——动态规划 var val = []; if (n == 1 || n == 2) { return 1; } else { val[1] = 1; val[2] = 2; for ( var i = 3; i <= n; ++i) { val[i] = val[i - 1] + val[i - 2]; } return val[n - 1]; } } alert(dynFib(7)); alert(recurFib(7));