题解 | #统计每个月兔子的总数#
统计每个月兔子的总数
https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
月份 1 2 3 4 5
数量 1 1 2 3 5
可知其为斐波那契数列
注意 索引 和 月份 差着1
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here while(line = await readline()){ let month = parseInt(line); let i = 3; // i 是下标,此时下标为3,其实是第4个月 let dp = [1,1,2]; // 初始化前三个月 if(month<dp.length){ console.log(dp[month-1]); }else{ while(i<month){ dp[i] = dp[i-1]+dp[i-2]; // 推到公式初始化整个dp数组 i++; } } console.log(dp[month-1]); // 最后一个月的数量 } }()