백준 번 문제 node.js 풀이입니다.
[node.js] 백준 14681번 사분면 고르기: 런타임 에러
fs 사용으로 인한 런타임에러
readline을 사용해 풀어야 함.
fs모듈을 사용한 풀이
const fs = require("fs");
const input = fs
.readFileSync('/dev/stdin')
.toString()
.split("\n");
const x = parseInt(input[0]);
const y = parseInt(input[1]);
function solution() {
if (x > 0 && y > 0) {
console.log("1");
} else if (x < 0 && y > 0) {
console.log("2");
} else if (x < 0 && y < 0) {
console.log("3");
} else if (x > 0 && y < 0) {
console.log("4");
}
}
solution();

fs모듈을 사용하면 위와 같이 특정 문제에서 런타임 에러가 발생한다.
readline모듈을 사용한 풀이
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
input.push(line);
}).on("close", function () {
const x = parseInt(input[0]);
const y = parseInt(input[1]);
function solution() {
if (x > 0 && y > 0) {
console.log("1");
} else if (x < 0 && y > 0) {
console.log("2");
} else if (x < 0 && y < 0) {
console.log("3");
} else if (x > 0 && y < 0) {
console.log("4");
}
}
solution();
process.exit();
});
오답 이유: 런타임 에러

사이트 문제로
백준 사이트 게시판 공지사항에선 readline 사용을 권장한다.
마무리
백준 사이트에서 readline 사용을 권장하나
fs모듈을 사용하는게 더 간결하고 편하다.
평상 시엔 fs모듈을 사용하고
런타임에러 발생 시 readline을 사용해야겠다.
'IT > 알고리즘' 카테고리의 다른 글
[node.js] 백준 15552번 빠른 A+B (0) | 2022.08.08 |
---|---|
[node.js] 백준 9498번 시험 성적 (0) | 2022.07.27 |
[node.js] 백준 25083번 새싹 (0) | 2022.07.26 |