✍ 문제 확인하기
접근 방법
- 잔돈은 (1000-입력값)
- 거스름돈 개수가 언제나 가장! 적게 되려면 잔돈으로 줄 수 있는 coin의 가격이 가장 큰 순서대로 나누어주면 됨
- coinList가 정렬되어 있지 않다면, 내림차순으로 정렬해준 후
- (현재 잔돈/coin)을 구해서 answer에 누적합하면 됨
- answer에 누적합한 후, 새로운 잔돈은 (현재 잔돈%coin)으로 구함
풀이
const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './data/5585.txt';
let input = fs.readFileSync(file).toString().trim().split('\n')*1;
function solution(coinList, n) {
let answer = 0;
let change = 1000-n;
for(let coin of coinList) {
answer += Math.floor(change/coin);
change = change%coin;
}
return answer;
}
const coinList = [500,100,50,10,5,1];
console.log(solution(coinList, input));
해설
- for문으로 coinList의 값에 접근하여 잔돈의 개수를 구한다!