题解 | #统计每个月兔子的总数#
https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
function countRabbit(str = '') {
let monthe = parseInt(str);
if (monthe < 3) return 1;
let one = 0;
let two = 0;
let other = 1;
for(let i = 3; i <= monthe; i++) {
// 算出 每个月, 三种兔子的数量
other += two; // 这个月 两个月大的兔子会生一个
two = one; // 这个月 上个月一月大的兔子变成两个月大
one = other; // 这个月 会生出一个月的兔子
}
return one + two + other;
}
console.log(countRabbit('3'))