<?php
header("Content-Type: text/plain; charset=utf-8");
$amount = 6600;
$notes_available = [
100 => 0,
200 => 3,
500 => 1,
2000 => 4,
5000 => 4,
];
krsort($notes_available, SORT_NUMERIC
);
function check_withdraw($notes, $nominal, $amount, $stack)
{
foreach($notes as $nominal => $note){
$temp_notes = $notes;
$temp_stack = $stack;
@$next_nominal = array_keys($temp_notes)[$key + 1];
$total_amount = 0;
foreach($stack as $total_nominal => $quantity){
$total_amount += $total_nominal*$quantity;
}
//если купюра есть и влезает - повторяем цикл с ней же
if(($total_amount + $nominal < $amount) and $temp_notes[$nominal]) {
$temp_stack[$nominal]++;
$temp_notes[$nominal]--;
$temp_stack = check_withdraw($temp_notes, $nominal, $amount, $temp_stack);
//если сумма соответствует - заканчивам цикл
}elseif(($total_amount + $nominal) == $amount and $temp_notes[$nominal]){
$temp_stack[$nominal]++;
$temp_notes[$nominal]--;
//иначе пробуем добавить купюру следующего номинала по убыванию
unset($temp_notes[$nominal]); $temp_stack = check_withdraw($temp_notes, $next_nominal, $amount, $temp_stack);
}else{
continue;
}
$check_stack_amount = 0;
foreach($temp_stack as $total_nominal => $quantity){
$check_stack_amount += $total_nominal*$quantity;
}
if($check_stack_amount==$amount){
return $temp_stack;
}
}
return $temp_stack;
}
function show_result($withdraw, $amount){
echo "Сумма $amount:\n\n";
echo "Выдача возможна, число купюр:\n\n";
foreach($withdraw as $nominal => $quantity){
echo $nominal."x".$quantity."\n";
}
}else{
echo "Выдача невозможна.";
}
}
show_result(check_withdraw($notes_available, false, $amount, $stack), $amount);
PD9waHAKaGVhZGVyKCJDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgiKTsKCiRhbW91bnQgPSA2NjAwOwoKJG5vdGVzX2F2YWlsYWJsZSA9IFsKICAgIDEwMCAgPT4gMCwKICAgIDIwMCAgPT4gMywKICAgIDUwMCAgPT4gMSwKICAgIDIwMDAgPT4gNCwKICAgIDUwMDAgPT4gNCwKXTsKCmtyc29ydCgkbm90ZXNfYXZhaWxhYmxlLCBTT1JUX05VTUVSSUMpOwoKJHN0YWNrID0gYXJyYXlfY29tYmluZShhcnJheV9rZXlzKCRub3Rlc19hdmFpbGFibGUpLCBhcnJheV9maWxsKDAsIGNvdW50KCRub3Rlc19hdmFpbGFibGUpLCAwKSk7CgpmdW5jdGlvbiBjaGVja193aXRoZHJhdygkbm90ZXMsICRub21pbmFsLCAkYW1vdW50LCAkc3RhY2spCnsKICAgIGZvcmVhY2goJG5vdGVzIGFzICRub21pbmFsID0+ICRub3RlKXsKCiAgICAgICAgJHRlbXBfbm90ZXMgPSAkbm90ZXM7CiAgICAgICAgJHRlbXBfc3RhY2sgPSAkc3RhY2s7CgogICAgICAgICRrZXkgPSBhcnJheV9zZWFyY2goJG5vbWluYWwsIGFycmF5X2tleXMoJHRlbXBfbm90ZXMpKTsKCiAgICAgICAgQCRuZXh0X25vbWluYWwgPSBhcnJheV9rZXlzKCR0ZW1wX25vdGVzKVska2V5ICsgMV07CgogICAgICAgICR0b3RhbF9hbW91bnQgPSAwOwoKICAgICAgICBmb3JlYWNoKCRzdGFjayBhcyAkdG90YWxfbm9taW5hbCA9PiAkcXVhbnRpdHkpewogICAgICAgICAgICAkdG90YWxfYW1vdW50ICs9ICR0b3RhbF9ub21pbmFsKiRxdWFudGl0eTsKICAgICAgICB9CgogICAgICAgIC8v0LXRgdC70Lgg0LrRg9C/0Y7RgNCwINC10YHRgtGMINC4INCy0LvQtdC30LDQtdGCIC0g0L/QvtCy0YLQvtGA0Y/QtdC8INGG0LjQutC7INGBINC90LXQuSDQttC1CiAgICAgICAgaWYoKCR0b3RhbF9hbW91bnQgKyAkbm9taW5hbCA8ICRhbW91bnQpIGFuZCAkdGVtcF9ub3Rlc1skbm9taW5hbF0pIHsKCiAgICAgICAgICAgICR0ZW1wX3N0YWNrWyRub21pbmFsXSsrOwogICAgICAgICAgICAkdGVtcF9ub3Rlc1skbm9taW5hbF0tLTsKICAgICAgICAgICAgJHRlbXBfc3RhY2sgPSBjaGVja193aXRoZHJhdygkdGVtcF9ub3RlcywgJG5vbWluYWwsICRhbW91bnQsICR0ZW1wX3N0YWNrKTsKCiAgICAgICAgLy/QtdGB0LvQuCDRgdGD0LzQvNCwINGB0L7QvtGC0LLQtdGC0YHRgtCy0YPQtdGCIC0g0LfQsNC60LDQvdGH0LjQstCw0Lwg0YbQuNC60LsKICAgICAgICB9ZWxzZWlmKCgkdG90YWxfYW1vdW50ICsgJG5vbWluYWwpID09ICRhbW91bnQgYW5kICR0ZW1wX25vdGVzWyRub21pbmFsXSl7CgogICAgICAgICAgICAkdGVtcF9zdGFja1skbm9taW5hbF0rKzsKICAgICAgICAgICAgJHRlbXBfbm90ZXNbJG5vbWluYWxdLS07CgogICAgICAgIC8v0LjQvdCw0YfQtSDQv9GA0L7QsdGD0LXQvCDQtNC+0LHQsNCy0LjRgtGMINC60YPQv9GO0YDRgyDRgdC70LXQtNGD0Y7RidC10LPQviDQvdC+0LzQuNC90LDQu9CwINC/0L4g0YPQsdGL0LLQsNC90LjRjgogICAgICAgIH1lbHNlaWYoaXNzZXQoJG5leHRfbm9taW5hbCkgYW5kIGFycmF5X3N1bShhcnJheV92YWx1ZXMoJHRlbXBfbm90ZXMpKSl7CgogICAgICAgICAgICB1bnNldCgkdGVtcF9ub3Rlc1skbm9taW5hbF0pOwogICAgICAgICAgICAkdGVtcF9zdGFjayA9IGNoZWNrX3dpdGhkcmF3KCR0ZW1wX25vdGVzLCAkbmV4dF9ub21pbmFsLCAkYW1vdW50LCAkdGVtcF9zdGFjayk7CgogICAgICAgIH1lbHNlewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CgogICAgICAgICRjaGVja19zdGFja19hbW91bnQgPSAwOwogICAgICAgIGZvcmVhY2goJHRlbXBfc3RhY2sgYXMgJHRvdGFsX25vbWluYWwgPT4gJHF1YW50aXR5KXsKICAgICAgICAgICAgJGNoZWNrX3N0YWNrX2Ftb3VudCArPSAkdG90YWxfbm9taW5hbCokcXVhbnRpdHk7CiAgICAgICAgfQogICAgICAgIGlmKCRjaGVja19zdGFja19hbW91bnQ9PSRhbW91bnQpewogICAgICAgICAgICByZXR1cm4gJHRlbXBfc3RhY2s7CiAgICAgICAgfQoKICAgIH0KICAgIHJldHVybiAkdGVtcF9zdGFjazsKfQoKZnVuY3Rpb24gc2hvd19yZXN1bHQoJHdpdGhkcmF3LCAkYW1vdW50KXsKICAgIGVjaG8gItCh0YPQvNC80LAgJGFtb3VudDpcblxuIjsKICAgIGlmKGFycmF5X3N1bSgkd2l0aGRyYXcpKXsKCiAgICAgICAgZWNobyAi0JLRi9C00LDRh9CwINCy0L7Qt9C80L7QttC90LAsINGH0LjRgdC70L4g0LrRg9C/0Y7RgDpcblxuIjsKICAgICAgICBmb3JlYWNoKCR3aXRoZHJhdyBhcyAkbm9taW5hbCA9PiAkcXVhbnRpdHkpewogICAgICAgICAgICBlY2hvICRub21pbmFsLiJ4Ii4kcXVhbnRpdHkuIlxuIjsKICAgICAgICB9CiAgICB9ZWxzZXsKICAgICAgICBlY2hvICLQktGL0LTQsNGH0LAg0L3QtdCy0L7Qt9C80L7QttC90LAuIjsKICAgIH0KfQoKc2hvd19yZXN1bHQoY2hlY2tfd2l0aGRyYXcoJG5vdGVzX2F2YWlsYWJsZSwgZmFsc2UsICRhbW91bnQsICRzdGFjayksICRhbW91bnQpOw==