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

统计每个月兔子的总数

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)
});

全部评论

相关推荐

Sigma429:极兔啊,薪资开的巨低,还在上海,索性不做笔试了
点赞 评论 收藏
分享
迷茫的大四🐶:💐孝子启动失败,改为启动咏鹅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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