본문 바로가기
알고리즘/Codewars

[백준] 26529: Bunnies / Node.js (JavaScript)

by fluss 2023. 4. 25.

https://www.acmicpc.net/problem/26529

 

26529번: Bunnies

You’re going to raise farm animals and you decided to start with bunnies, the easiest of animals. To your surprise they are breeding like rabbits, so much so that you’re unable to count them accurately. However, you know that rabbits’ breeding patter

www.acmicpc.net

 

문제

You’re going to raise farm animals and you decided to start with bunnies, the easiest of animals. To your surprise they are breeding like rabbits, so much so that you’re unable to count them accurately. However, you know that rabbits’ breeding patterns always follow the Fibonacci sequence. The Fibonacci sequence is defined as follows:

F(0) = 1, F(1) = 1, F(N) = F(N-1)+F(N-2)

Given the number of months the rabbits have been breeding, use the Fibonacci sequence to determine the number of rabbits you should have.

 

입력

The first line will contain a single integer n that indicates the number of data sets that follow. Each data set will start with a single integer x denoting the number of months that have passed since you bought your initial pair of rabbits. 0≤x≤45

 

출력

For each test case, output the expected number of rabbits after x months.

 

예제 입력 1

3
0
5
45

 

예제 출력 1

1
8
1836311903

 

코드

const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n').map(Number);
const n = input[0];
const max = 490;
const dp = Array(max + 1).fill(0n);
let result = '';
dp[1] = 1n;
for(let i = 2; i <= max; i++){
    dp[i] = dp[i - 1] + dp[i - 2];
}

for(let i = 1; i <= n; i++){
    result += dp[input[i] + 1].toString() + '\n';
}
console.log(result.trim());

댓글