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

统计每个月兔子的总数

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

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let total = 1;
let childenBirthMonth = []

const getChildren = (birthMonth, month) => {
    for (let i = birthMonth; i <= month; i++) {
        if (i - birthMonth >= 2) {
            total++;
            childenBirthMonth.push(i);
        }
    }
}

rl.on('line', function (line) {
    const month = parseInt(line);

    getChildren(1, month);

    for (let i = 0; i < childenBirthMonth.length; i++) {
        getChildren(childenBirthMonth[i], month);
    }

    console.log(total);
});

全部评论

相关推荐

你背过凌晨4点的八股文么:简历挂了的话会是流程终止,像我一样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务