<?php
/**
* coin changer
*
* usage:
* $coin = 179;
* $ary = array(10,50,5,100);
* $result = coin_changer($coin, $ary);
* @param int $raw_coin
* @param array $coin_amount_ary
* @return array
*/
function coin_changer($raw_coin = 0, $coin_amount_ary = array()) {
$coin_ary = array_keys($coin_amount_ary);
$result_ary = array();
rsort($coin_ary);
$i = 0;
foreach ($coin_ary as $coin) {
if ($raw_coin <= 0) {
break;
}
$cnt = intval($raw_coin / intval($coin)); //張數
$raw_coin %= intval($coin); //餘額
$result_ary[$coin] = $cnt;
}
return $result_ary;
}
$coin = 179;
$ary = array(10,50,5,100);
$result = coin_changer($coin, $ary);
print_r($result);
留言