[Lv.0] 주사위의 개수 *

01수정·2022년 11월 14일
0
post-thumbnail

<입문 100문제> Day 11 - 수학, 반복문

문제


풀이

(1) 부피로만 비교 : X
주사위가 물처럼 유동적이라서 비어 있는 공간을 채울 수 있다면 아래도 가능하겠지만, 고체이므로 불가능하다

function solution(box, n) {
    let box_volume = box[0] * box[1] * box[2]
    let dice_volume = n * n * n

    return Math.floor(box_volume / dice_volume)
}

(2) 각각의 면을 비교 : O

function solution (box, n) {
    return box.reduce((count, length) => { return count *= Math.floor(length/n) }, 1)
}

해답

function solution(box, n) {
  return Math.floor(box[0] / n) * Math.floor(box[1] / n) * Math.floor(box[2] / n);
}
profile
새싹 FE 개발자

0개의 댓글