[백준 1003] 다이나믹 프로그래밍 - 피보나치 함수

김민지·2023년 3월 20일
0

냅다 시작 백준

목록 보기
30/118

✨ 문제 ✨

✨ 정답 ✨

// const { json } = require("express/lib/response");
// const fs = require("fs");
// const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
// let input = fs.readFileSync(filePath).toString().trim().split('\n');
// // const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n')
// // const line = require("fs").readFileSync("/dev/stdin", "utf8");
// // let input = line.trim().split("\n"); 

// input=input.map((el)=> parseInt(el.trim()))
// let N=input[0];
// let array=[[1,0], [0,1], [1,1], [1,2], [2, 4]]

// for (let i=1;i<=N;i++){
//     let M=input[i];
//     for (let j=5;j<=M;j++){
//         array[j][0]=array[j-1][0]+array[j-2][0]
//         array[j][1]=array[j-1][1]+array[j-2][1]
//     }
//     console.log(array[M][0],array[M][1])
// }



// const { json } = require("express/lib/response");
// const fs = require("fs");
// const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
// const input2 = fs.readFileSync(filePath).toString().trim().split('\n');



// let input=[];
// input=input2.map((el)=> parseInt(el.trim()))
// console.log('input', input)
// let N=input[0];
// let array=[[1,0], [0,1], [1,1], [1,2], [2, 4]]

// for (let i=1;i<=N;i++){
//     let M=input[i];
//     for (let j=5;j<=M;j++){
//         array[j][0]=array[j-1][0]+array[j-2][0]
//         array[j][1]=array[j-1][1]+array[j-2][1]
//     }
//     console.log(array[M].join(' '))
// }


const input2 = require('fs').readFileSync('/dev/stdin').toString().trim().split("\n")

let input=[];
input=input2.map((el)=> parseInt(el));
let N=input[0];
let array=[[1,0], [0,1]];

for (let i=1;i<=N;i++){
    let M=input[i];
    for (let j=2;j<=M;j++){

        // array[j][0]=array[j-1][0]+array[j-2][0];
        // array[j][1]=array[j-1][1]+array[j-2][1];
        array[j]=[
            array[j-1][0]+array[j-2][0],
            array[j-1][1]+array[j-2][1]
        ];
    }
    console.log(array[M].join(' '));
}
profile
이건 대체 어떻게 만든 거지?

0개의 댓글