<?php
header("Content-Type: text/plain; charset=utf-8");
$amount = 5600;
$notes_available = [
100 => 0,
200 => 3,
500 => 1,
2000 => 4,
5000 => 1,
];
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);
$amount2 = 600;
$withdraw = check_withdraw($amount2, $notes_available);
get_message($withdraw, $amount2);
PD9waHAKaGVhZGVyKCJDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgiKTsKCiRhbW91bnQgPSA1NjAwOwoKJG5vdGVzX2F2YWlsYWJsZSA9IFsKICAgIDEwMCAgPT4gMCwKICAgIDIwMCAgPT4gMywKICAgIDUwMCAgPT4gMSwKICAgIDIwMDAgPT4gNCwKICAgIDUwMDAgPT4gMSwKXTsKCmty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PRjtGJ0LjQtSDQuNGC0LXRgNCw0YbQuNC4ICovCiAgICAgICAgdW5zZXQoJG5vdGVzWyRub21pbmFsXSk7CgogICAgICAgIC8qINGB0L7Qt9C00LDRjiDQvtGC0LTQtdC70YzQvdGL0Lkg0LzQsNGB0YHQuNCyINGBINCy0LDQu9GO0YLQsNC80Lgg0LTQu9GPINGB0LvQtdC00YPRjtGJ0LXQuSDQuNGC0LXRgNCw0YbQuNC4LAogICAgICAgICAgINC/0L7RgtC+0LzRgyDRh9GC0L4g0YLQtdC60YPRidC40Lkg0LHRg9C00LXRgiDQuNGB0L/QvtC70YzQt9C+0LLQsNGC0YzRgdGPINC00LDQu9GM0YjQtSDQsiBmb3JlYWNoICovCiAgICAgICAgJG5leHRfY3VycmVudF9ub3RlcyA9ICRjdXJyZW50X25vdGVzOwogICAgICAgIAogICAgICAgIC8qINC10YHQu9C4INC60YPQv9GO0YAgPiAwLCDQtNC+0LHQsNCy0LvRj9C10Lwg0LjRhSDQutC+0LvQuNGH0LXRgdGC0LLQviDQuCDQvdC+0LzQuNC90LDQuyDQsiDRgtC10LrRg9GJ0LjQuSDQvdCw0LHQvtGAINCyINC40YLQtdGA0LDRhtC40LggKi8KICAgICAgICBpZigkY3VycmVudF9xdWFudGl0eSkKICAgICAgICAgICAgJG5leHRfY3VycmVudF9ub3Rlc1skbm9taW5hbF0gPSAkY3VycmVudF9xdWFudGl0eTsKCiAgICAgICAgLyog0LXRgdC70Lgg0L7RgdGC0LDRgtC+0Log0YDQsNCy0LXQvSAwIC0g0LTQvtCx0LDQstC70Y/QtdC8INCyINC80LDRgdGB0LjQsiDQstGL0LTQsNGH0LggKi8KICAgICAgICBpZighKCRhbW91bnQgLSAkY3VycmVudF9zdW1tKSl7CiAgICAgICAgICAgICR3aXRoZHJhd1tdID0gJG5leHRfY3VycmVudF9ub3RlczsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgY2hlY2tfd2l0aGRyYXcoJGFtb3VudCAtICRjdXJyZW50X3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