<?php /** * No.32 貯金箱の憂鬱 * * @see https://yukicoder.me/problems/5 */ const CURRENCY_UNIT_LIST = [1000, 100, 25, 1]; $numOfCoinHandred = trim(fgets(STDIN)); $numOfCoinTwentyFive = trim(fgets(STDIN)); $numOfCoinOne = trim(fgets(STDIN)); $total = CURRENCY_UNIT_LIST[1] * $numOfCoinHandred + CURRENCY_UNIT_LIST[2] * $numOfCoinTwentyFive + CURRENCY_UNIT_LIST[3] * $numOfCoinOne; excangeMoney($total, CURRENCY_UNIT_LIST, $results); // echo implode(",", $results) . "\n"; $numOfCoin = $results[1] + $results[2] + $results[3]; // 出力 echo $numOfCoin . "\n"; function excangeMoney($total, $currencyUnitList, &$numListOfEachUnit) { rsort($currencyUnitList); if (count($currencyUnitList) == 0) { return; } $targetUnit = array_shift($currencyUnitList); $num = floor($total / $targetUnit); $numListOfEachUnit[] = $num; $total -= $targetUnit * $num; excangeMoney($total, $currencyUnitList, $numListOfEachUnit); }