[백준] 5585 거스름돈 - Node.js

송철진·2023년 4월 6일
0

백준-Node.js

목록 보기
56/69

문제

https://www.acmicpc.net/problem/5585

Solution

const input = require('fs').readFileSync('/dev/stdin').toString().trim()
const arr = [500, 100, 50, 10, 5, 1]

const solution = (arr, input) => {
  let rest = 1000 - input
  let count = 0
  arr.forEach((el,i) => {
    count += parseInt(rest / el)
    rest = rest % el
  })
  return count
}

console.log(solution(arr, input))

500엔부터 1엔까지 내림차순으로
거스름돈rest를 나누어서 몫은 개수count로 더해주고
나머지는 거스름돈rest에 재할당하여 반복한다.

profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글