백준에 처음 입문한 사람이라면 백준 입출력 방식에 난감한 경험이 있을겁니다.
Javascript로 알고리즘 준비를 하고 있어서 node.js로 작성하는 팁을 공유합니다.
const input = require("fs")
.readFileSync("example.txt", "utf8")
.trim()
.split("\n");
const solution = (input) => {};
solution(input);
example.txt
백준
1 2 3 4 5
// index.js
const input = require("fs")
.readFileSync("example.txt", "utf8")
.trim()
.split("\n");
const solution = (input) => {
console.log(input);
};
solution(input);
이렇게 작성하고 터미널에 node index.js 입력 후 출력
위에서 작성한 코드는 txt파일에 있는 예제값을 받아서 처리하는거기때문에 백준 사이트에서 작성할때는 input을 다르게 받아줘야합니다
const input = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n");
const solution = (input) => {};
solution(input);
solution에 있는 코드는 그대로 두고 input 변수에 들어간 코드만 저렇게 바꿔주면 그대로 백준 사이트에서 정답을 확인할 수 있습니다.
도움이 됐다면 ❤️