언젠가는 펼쳐 볼 아카이브

[BOJ] 14681번 - 사분면 고르기 본문

IT/Baekjoon Oline Judge

[BOJ] 14681번 - 사분면 고르기

개발자희망생고롸파덕 2023. 8. 15. 18:41

사용언어 : javascript - node.js

 

# 처음 제출한 코드

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(filePath).toString().split('\n');

solution(input[0], input[1]);

function solution(num1, num2) {
  const n1 = Number(num1);
  const n2 = Number(num2);

  if (n1 > 0 && n2 > 0) {
    console.log('1');
  } else if (n1 < 0 && n2 > 0) {
    console.log('2');
  } else if (n1 < 0 && n2 < 0) {
    console.log('3');
  } else {
    console.log('4');
  }
}

>> 결과 : 런타임 에러(EACCES)

 

잉.. 왜죠ㅜㅜ? VS 에서는 정상적으로 나왔는데..

찾아보니 백준 온라인에서는 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 () {
  input = input.map((item) => +item);
  solution(input[0], input[1]);
  process.exit();
});

function solution(x, y) {
  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 {
    console.log(4);
  }
}

 

'IT > Baekjoon Oline Judge' 카테고리의 다른 글

[BOJ] 2525 - 오븐 시계  (0) 2023.08.15
[BOJ] 2884 - 알람시계  (0) 2023.08.15
[BOJ] 2753번 - 윤년  (0) 2023.08.15
[BOJ] 1330번 - 두 수 비교하기  (0) 2023.08.15
[BOJ] 10171번 - 고양이  (0) 2023.08.15