<?php
header("Content-Type: text/plain; charset=utf-8");
$amount = 11000;
$notes_available = [
100 => 1,
200 => 1,
500 => 1,
2000 => 4,
5000 => 4,
];
krsort($notes_available, SORT_NUMERIC
);
function check_withdraw($amount, $notes, $current_notes = []){
static $withdraw = [];
foreach($notes as $nominal => $quantity){
/* вычисляем максимальное количество банкнот текущего номинала для выдачи остатка */
$needed = (int)($amount / $nominal);
if ($quantity >= $needed):
$current_quantity = $needed;
else:
$current_quantity = $quantity;
endif;
/* вычисляем сумму для выдачи текущей банкнотой */
$current_summ = $current_quantity * $nominal;
/* убираем использованную валюту из массива, чтобы передать остальные в следующие итерации */
/* создаю отдельный массив с валютами для следующей итерации,
потому что текущий будет использоваться дальше в foreach */
$next_current_notes = $current_notes;
/* если купюр > 0, добавляем их количество и номинал в текущий набор в итерации */
if($current_quantity)
$next_current_notes[$nominal] = $current_quantity;
/* если остаток равен 0 - добавляем в массив выдачи */
if(!($amount - $current_summ)){
$withdraw[] = $next_current_notes;
}
check_withdraw($amount - $current_summ, $notes, $next_current_notes);
}
/* на выходе получаются все возможные комбинации купюр, при обработке я беру первую */
return $withdraw;
}
function get_message($withdraw, $amount){
echo "Сумма $amount:\n";
if(!empty($withdraw[0])){
echo "Выдача возможна, число купюр:\n";
foreach($withdraw[0] as $nominal => $quantity){
echo $nominal."x".$quantity." ";
}
}else{
echo "Выдача невозможна.";
}
}
$withdraw = check_withdraw($amount, $notes_available);
get_message($withdraw, $amount);
PD9waHAKaGVhZGVyKCJDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgiKTsKCiRhbW91bnQgPSAxMTAwMDsKCiRub3Rlc19hdmFpbGFibGUgPSBbCiAgICAxMDAgID0+IDEsCiAgICAyMDAgID0+IDEsCiAgICA1MDAgID0+IDEsCiAgICAyMDAwID0+IDQsCiAgICA1MDAwID0+IDQsCl07CgprcnNvcnQoJG5vdGVzX2F2YWlsYWJsZSwgU09SVF9OVU1FUklDKTsKCmZ1bm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