题解 | #统计每个月兔子的总数#
统计每个月兔子的总数
https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.on('line', function (line) { // 第n个月 const n = parseInt(line, 10) // 1月有一只兔子,第三个月起开始生兔子 // s[1] = 0 // s[2] = 0 // s[3] = 1个生产的兔子 // s[4] = 1个生产的兔子 // s[5] = 2个生产的兔子 // s[6] = 3个生产的兔子 // s[7] = 5个生产的兔子 // 出生数量 s[n] = 前一个月生产的兔子 + 前两个月出生的兔子 const s = Array.from({length: n + 1}, () => 0) for (let i=3; i<s.length; i+=1) { if (i===3) { s[i] = 1 } else { s[i] = s[i-1]+s[i-2] } } const total = s.reduce((a, b) => a+b) + 1 console.log(total) });