BOJ | #14681 "사분면 고르기"

블로그 이사 완료·2022년 9월 18일
0
post-thumbnail

문제


Code

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


const a = parseInt(input[0]);
const b = parseInt(input[1]);

if (a > 0 && b > 0) {
  console.log(1);
} else if (a < 0 && b > 0) {
  console.log(2);
} else if (a < 0 && b < 0) {
  console.log(3);
} else if (a > 0 && b < 0) {
  console.log(4);
}

Review

두 수를 숫자배열로 변환해 각 값이 0보다 큰지 작은지 조건을 따져 출력하는 문제였다.

다른 사람은 삼항연산자를 사용해 더 간단하게 코드를 작성했다.

const fs = require('fs');

const [x, y] = fs.readFileSync(0).toString().trim().split('\n').map(Number);

if(x > 0) y > 0 ? console.log(1) : console.log(4)
if(x < 0) y > 0 ? console.log(2) : console.log(3)

profile
https://kyledev.tistory.com/

0개의 댓글