백준 11047번 node.js 해결
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
let [n, k] = (input[0]).split(' ').map(Number);
const coinList = [];
let result = 0;
//비교할 동전 종류 목록
for(let i = 1 ; i <= n ; i += 1 ){
coinList.push(Number(input[i]));
}
for(let j = n - 1 ; j >= 0 ; j -= 1){
//각각의 동전 사용 개수
result += parseInt(k / coinList[j]);
//큰 동전 이용 후 남은 거스름돈
k %= coinList[j];
}
console.log(result);
각 동전 종류끼리 배수 관계가 존재한다는 조건이 있습니다. 가치가 큰 동전은 가치가 작은 동전의 합으로 표현될 수 있다는 점이 보장됩니다.
가치가 큰 동전부터 개수를 우선 구한 후, 거스름돈을 계산해 다음으로 가치가 큰 동전의 개수를 구해줍니다.