有一对兔子,从出生后第三个月起每个月都生一对兔子,小兔子长到三个月后每个月又生一对兔子,假如兔子都不死,问一年中每个月各有多少对兔子?
let [rabbits, months] = [[0], [...new Array(12)]];
const result = months.reduce(counts => {
rabbits = rabbits.reduce((origin, item) => {
origin.push(++item);
return origin;
}, []);
rabbits.forEach(rabbit => rabbit >= 3 && rabbits.push(1));
counts.push(rabbits.length);
return counts;
}, []);
console.log(result); // [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ]
以上为代码演示,欢迎指正!