문제
2720번: 세탁소 사장 동혁
각 테스트케이스에 대해 필요한 쿼터의 개수, 다임의 개수, 니켈의 개수, 페니의 개수를 공백으로 구분하여 출력한다.
www.acmicpc.net
정답
let fs = require("fs");
let filePath = process.platform === "linux" ? "/dev/stdin" : "test.txt";
let changes = fs.readFileSync(filePath).toString().trim().split("\n").map(Number);
// 동전을 센트 단위로 바꿔 배열화한다
const quarter = 25;
const dime = 10;
const nickel = 5;
const penny = 1;
const coinArr = [quarter, dime, nickel, penny];
// 동전의 배열을 순회하며 각각의 동전에 대한 몫을 최대로 구하는 반복문을 만든다
function handleChange(change) {
let totalChange = change;
let totalCoinCount = "";
for (let i = 0; i < coinArr.length; i++) {
const coin = coinArr[i];
if (totalChange / coin >= 0) {
const coinCount = Math.floor(totalChange / coin);
totalCoinCount += coinCount + " ";
totalChange = totalChange - coin * coinCount;
}
}
return totalCoinCount;
}
// 예제의 첫 번째 값인 3을 제외하고 정답을 출력한다
changes.map((change, index) => {
if (index > 0) console.log(handleChange(change));
});