BOJ | #2480 "주사위 세개"

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

문제


Code

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

const [a, b, c] = [...input];

const answer = (a, b, c) => {
  if (a === b && a === c && b === c) return console.log(10000 + a * 1000);

  if (a !== b || b !== c || a !== c) {
    if (a === b || a === c) return console.log(1000 + a * 100);
    if (b === c) return console.log(1000 + b * 100);
  }

  if (a !== b || b !== c || a !== c) {
    maxNum = Math.max(a, b, c);
    return console.log(maxNum * 100);
  }
};

answer(a, b, c);

Review

세개의 조건 [3개 일치, 2개 일치, 불일치] 의 조건문까지는 작성 할 수 있었다.

[3개 일치] 조건은 a,b,c가 모두 일치하므로 아무 값이나 출력 값으로 사용하면 됐다.

그런데 [2개 일치] 조건에서 그 일치되는 값을 찾는 것이 어려웠다. 결국 다른사람 풀이법을 보고 그 안에 추가의 조건문을 작성하는 법을 찾았다.

  • a와 일치하는 조건 또는 b와 일치하는 조건 추가

마지막으로 [불일치] 조건은 간단했다.
Math.max() 로 a,b,c 중에 최대값을 변수에 할당해서 문제의 출력 값을 리턴했다.


profile
https://kyledev.tistory.com/

0개의 댓글