문제
https://www.acmicpc.net/problem/1003
1003번: 피보나치 함수
각 테스트 케이스마다 0이 출력되는 횟수와 1이 출력되는 횟수를 공백으로 구분해서 출력한다.
www.acmicpc.net
문제풀이
해당 문제를 조금 잘라서 나타냈는데 결국 피보나치 수열을 진행하면서 f(0)과 f(1)을 각 몇 번씩 사용하는지 출력하는 문제이다.
해당 문제는 피보나치 수열을 구하는 DP문제에서 값을 저장하는 것이 아닌 f(0)과 f(1)을 사용하는 횟수를 저장하면 된다.
제출 코드
const fs = require('fs');
const stdin = (process.platform === 'linux'? fs.readFileSync('/dev/stdin').toString() :
`2
6
22`).split('\n');
const input = (() => {
let line = 0;
return () => stdin[line++];
})();
let c = Number(input());
let num = [];
for(let i = 0; i < c; i++) num.push(Number(input()));
let pibo = [[1,0],[0,1]]
while(num.length){
const x = num.shift();
for(let i = pibo.length; i <= x; i++){
pibo.push([pibo[i-1][0] + pibo[i-2][0],pibo[i-1][1] + pibo[i-2][1]])
}
console.log(pibo[x].join(' '));
}
'algorithm > problems' 카테고리의 다른 글
[백준]1107번 리모컨 (0) | 2022.11.26 |
---|---|
[백준] 1072번 Z _ Node_js (0) | 2022.11.21 |
[백준] 18111번 마인크래프트 _ Node.js (0) | 2022.11.19 |
[백준] 6568번 덩치 _ Node (1) | 2022.11.16 |
[백준] 2805번 나무 자르기 _ Node.js (0) | 2022.11.15 |