Posts

백준 | #14681 "사분면 고르기"

목차4

문제


나의 풀이

코드
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);
}

다른 풀이

코드
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)

마무리

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

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

반응형