<?php
//Банкомат
//требуемая сумма
$amount = 1100;
$errorMsg = "Выдача невозможна: ";
$successMsg = "Выдача возможна, число купюр:\n";
//запас наличных
$bills = [
100 => 23,
500 => 5,
1000 => 0,
5000 => 20
];
echo "Сумма: {$amount}\n";
if ($amount < 0) {
echo $errorMsg . "Сумма меньше 0";
}
if ($amount % 100 != 0) {
echo $errorMsg . "Сумма не кратна 100";
}
$cash = [];
function countBills($amount, $denomination, $billsInATM)
{
$billsRequired = floor($amount / $denomination); if ($billsRequired <= $billsInATM) {
return $billsRequired;
} else {
return $billsInATM;
}
}
foreach ($bills as $key => $value) {
$bill = countBills($amount, $key, $value);
if ($bill > 0) {
$cash[$key] = $bill;
$amount = $amount - ($bill * $key);
}
}
if ($amount != 0) {
echo $errorMsg . "В банкомате недостаточно купюр";
}
foreach ($cash as $key => $value) {
$successMsg .= "{$value}x{$key} ";
}
echo $successMsg;
PD9waHAKCi8v0JHQsNC90LrQvtC80LDRggplcnJvcl9yZXBvcnRpbmcoLTEpOwptYl9pbnRlcm5hbF9lbmNvZGluZygndXRmLTgnKTsKCi8v0YLRgNC10LHRg9C10LzQsNGPINGB0YPQvNC80LAKJGFtb3VudCA9IDExMDA7CiRlcnJvck1zZyA9ICLQktGL0LTQsNGH0LAg0L3QtdCy0L7Qt9C80L7QttC90LA6ICI7CiRzdWNjZXNzTXNnID0gItCS0YvQtNCw0YfQsCDQstC+0LfQvNC+0LbQvdCwLCDRh9C40YHQu9C+INC60YPQv9GO0YA6XG4iOwovL9C30LDQv9Cw0YEg0L3QsNC70LjRh9C90YvRhQokYmlsbHMgPSBbCiAgICAxMDAgID0+IDIzLAogICAgNTAwICA9PiA1LAogICAgMTAwMCA9PiAwLAogICAgNTAwMCA9PiAyMApdOwoKZWNobyAi0KHRg9C80LzQsDogeyRhbW91bnR9XG4iOwoKa3Jzb3J0KCRiaWxscyk7CgppZiAoJGFtb3VudCA8IDApIHsKICAgIGVjaG8gJGVycm9yTXNnIC4gItCh0YPQvNC80L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