题目:
思路:
1 2 3 4 5
1 1 2 3 5
斐波那契数列
dp[i] = dp[i - 1] + dp[i - 2]
提交:
while(line = Number(readline())) {
// 递归
// function getCurrentMonthNum(month) {
// if (month <= 2) {
// return 1
// } else {
// return getCurrentMonthNum(month - 1) + getCurrentMonthNum(month - 2)
// }
// }
// console.log(getCurrentMonthNum(line))
// 改进
let arr = [1, 1]
function getCurrentMonthNumNoRecursion(month) {
if (month <= 2) {
return 1
}
for (let i = 3; i <= month; i++) {
arr[i - 1] = arr[i - 2] + arr[i - 3]
}
return arr[month - 2] + arr[month - 3]
}
console.log(getCurrentMonthNumNoRecursion(line))
}