题解 | 统计每个月兔子的总数

统计每个月兔子的总数

https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    const month = +await readline();
    let index = 1; // 当前计算月数
    let t1 = 1, t2 = 0, t3 = 0; // 分别代表1、2个月和大于等于3个月的兔子数量

    while(index < month) {
        index++; // 加一个月,计算数量
        t3 += t2; // 3个月的等于2个月的
        t2 = t1; // 2个月的等于一个月的
        t1 = t3; // 1个月的等于3月的(每只都生一只)
    }
    console.log(t1+t2+t3);
}()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务