[백준 문제풀이] 2480 주사위 세 개 (Javascript, python)

방예서·2022년 5월 18일
0

코딩테스트 준비

목록 보기
6/37

주사위 세개

문제를 잘 읽고 그대로 조건문을 작성하면 되는 문제였다.
두 가지만 같은 부분의 조건을 일일이 작성해도 되나가 고민 포인트였다.
좋은 방법이 떠오르지 않아 일단 두 개가 같은 조건문을 하나 쓰고, 그 안에 두 개의 조건문으로 나눠주었다.


  • python
# 백준 2480 주사위 세개 python

import sys
input = sys.stdin.readline


a, b, c = map(int, input().split())

result = 0

if a == b == c:
    result = 10000 + 1000 * a
elif a == b or b == c or c == a:
    if a == b:
        result = 1000 + 100 * a
    else:
        result = 1000 + 100 * c
else:
    result = 100 * max(a, b, c)

print(result)
  • JS

// 2480 js


const fs = require('fs');
const input = fs.readFileSync("input.txt").toString().trim().split(" ");

const diceNum = input;
const maxDice = Math.max(...diceNum);

let result = 0;

function compareDice(a, b, c) {
  if (a === b && b === c && c === a) {
    result = 10000 + 1000 * a;
  } else if (a === b || b === c || c === a) {
    if (a === b) {
      result = 1000 + 100 * a;
    } else {
      result = 1000 + 100 * c;
    }
  } else {
    result = 100 * maxDice;
  }

  return result;
}

console.log(compareDice(diceNum[0], diceNum[1], diceNum[2]))

파이썬과 매우 유사하다. max 부분이 약간 다른데, 스터디 후 코드를 리팩토링 해보았다.

// 2480 js


const fs = require('fs');
const input = fs.readFileSync("input.txt").toString().trim().split(" ");

const diceNum = input;
// const maxDice = Math.max(...diceNum);

let result = 0;

function compareDice(a, b, c, arr) {
  if (a === b && b === c && c === a) {
    result = 10000 + 1000 * a;
  } else if (a === b || b === c || c === a) {
    if (a === b) {
      result = 1000 + 100 * a;
    } else {
      result = 1000 + 100 * c;
    }
  } else {
    result = 100 * Math.max(...arr);
  }

  return result;
}

console.log(compareDice(diceNum[0], diceNum[1], diceNum[2], diceNum))

뭐가 더 나은지는... 모르겠다. 😊

profile
console.log('bang log');

0개의 댓글