<?php
$money=54700;
echo "Сумма: $money рублей\n";
$result = cnt($money);
switch ($result) {
case -1:
echo "Выдача невозможна: введено отрицательное значение";
break;
case -2:
echo "Выдача невозможна: введено неверное значение";
break;
case -3:
echo "Выдача невозможна: в банкомате недостаточно денег";
break;
default:
echo "Выдача возможна, число купюр:\n$result[1]x5000 $result[2]x1000 $result[3]x500 $result[4]x100";
}
function cnt($money) {
1 => 23,
2 => 4,
3 => 9,
4 => 200);
#подсчитываем имеющееся количество
$moneyHave=(
$bills[1]*100 +
$bills[2]*500 +
$bills[3]*1000 +
$bills[4]*5000);
#обработчик ошибок
if ($money<0) {return -1;}
if ($money % 100 != 0) {return -2;}
if ($money>$moneyHave) {return -3;}
$moneyPaid=0; //деньги, которые потрачено
$money2=$money; //переменная для счета
for ($i=1;$moneyPaid<$money;$i++) {
if (($money2>5000)&&($bills[1]>0)) {
$moneyPaid+=5000;
$money2-=5000;
$bills[1]--;
$uses[1]++;
}
elseif (($money>1000)&&($bills[2]>0)) {
$moneyPaid+=1000;
$money2-=1000;
$bills[2]--;
$uses[2]++;
}
elseif (($money>500)&&($bills[3]>0)) {
$moneyPaid+=500;
$money2-=500;
$bills[3]--;
$uses[3]++;
}
elseif (($money>=100)&&($bills[4]>0)) {
$moneyPaid+=100;
$money2-=100;
$bills[4]--;
$uses[4]++;
}
}
return $uses;
}
PD9waHAKCiRtb25leT01NDcwMDsKZWNobyAi0KHRg9C80LzQsDogJG1vbmV5INGA0YPQsdC70LXQuVxuIjsKCiRyZXN1bHQgPSBjbnQoJG1vbmV5KTsKc3dpdGNoICgkcmVzdWx0KSB7CgljYXNlIC0xOgoJCWVjaG8gItCS0YvQtNCw0YfQsCDQvdC10LLQvtC30LzQvtC20L3QsDog0LLQstC10LTQtdC90L4g0L7RgtGA0LjRhtCw0YLQtdC70YzQvdC+0LUg0LfQvdCw0YfQtdC90LjQtSI7CgkJYnJlYWs7CgljYXNlIC0yOgoJCWVjaG8gItCS0YvQtNCw0YfQsCDQvdC10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