[백준 2480 주사위 세계]

초록귤·2022년 4월 3일
0

JS코테

목록 보기
2/6
post-thumbnail

오랜만에 코테를 다시 준비해보니, 처음부터 다시 공부의 필요성을 느꼈다. 😇 ( 매번 꾸준히가 답이란 것을 깨닫는 중...)
특히, js로 풀어보는게 웹 개발에도 많은 도움이 될거란 생각에 도전하게 되었다.

반복문

const fs = require("fs");
const input = fs
  .readFileSync("/dev/stdin")
  .toString()
  .split(" ")
  .map((v) => +v);
// const input = [6, 2, 5];
let sameCount = [];
for (i = 0; i <= 2; i++) {
  for (j = i + 1; j < input.length; j++) {
    // console.log(`i값 : ${i} j값 : ${j}`);
    if (input[i] == input[j]) {
      // console.log(`i값 ${input[i]} j값${input[j]}`);
      sameCount.push(input[i]);
    }
  }

  if (sameCount.length === 2) {
    break;
  }
}
// console.log(`sameCount: ${sameCount}`);
Value = Number(sameCount[0]);
// console.log(Value);
if (sameCount.length == 2) {
  console.log(10000 + Value * 1000);
} else if (sameCount.length == 1) {
  console.log(1000 + Value * 100);
} else {
  maxValue = Math.max(...input);
  console.log(maxValue * 100);
}

sort

더 좋은 방법을 찾다가 sort로 정렬한 뒤에 비교하면 더 간단해진다는 것을 알게 되었다.
sort에서 (a,b) => a-b 로 쓴다면 아스키 코드 순서로 정렬이 되기 때문에, 숫자의 오름차순 출력이 일어나지 않아 틀리는 경우를 만날 수 있다. 숫자 오름차순은 꼭 (a,b) => a-b 써주자

const fs = require("fs");
const [a, b, c] = fs
  .readFileSync("/dev/stdin")
  .toString()
  .split(" ")
  .map(Number)
  .sort((a, b) => a - b);

if (a === b && b === c) {
  console.log(10000 + a * 1000);
} else if (a === b || b === c) {
  console.log(1000 + b * 100);
} else {
  console.log(c * 100);
}
profile
초록색 귤이 노랑색으로 익어가듯, 실력이 익어가기 위해 노력하는 개발자 lahee입니다. 프론트엔드 개발자를 목표로 성장하고 있습니다.

0개의 댓글