【简单】算法nodeJs:统计每个月兔子的总数

88 阅读1分钟

有一种兔子,从出生后第三个月起,每个月都会生一只兔子,生出来的兔子同理。假设兔子都不死,求解第 n 个月时的兔子总数。

输入描述:

在一行上输入一个整数 n(1≦n≦31) 代表查询的月份。

输出描述:

在一行上输出一个整数,代表第 n 个月的兔子总数。

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

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        const fn = (m) => {
            if (m < 3) return 1;
            else return fn(m - 1) + fn(m - 2);
        };
        console.log(fn(Number(line)));
    }
})();