<?php
/*Given data*/
$amount = 6600;
100 => 23,
200 => 3,
500 => 1,
1000 => 0,
2000 => 4,
5000 => 1
);
/*Calculate resulting greenbacks*/
function countResult($amount, $arr)
{
foreach ($greenback as $item) {
$count = 0;
$res = 0;
while ($count < $arr[$item] && $res <= $amount) {
$res += $item;
$count = $res / $item - 1;
}
$result[$item] = $count;
$amount = $amount - $item * $count;
}
}
/*Check for undefined possibilities*/
function checker($amount, $arr)
{
$availableSum = 0;
foreach ($arr as $greenback => $count) {
$availableSum += $greenback * $count;
}
$errorMsg = "";
if ($amount < 0) {
$errorMsg .= "сумма меньше нуля (0)";
} elseif ($amount % 100 !== 0 ) {
$errorMsg .= "сумма не кратна ста (100)";
} elseif ($amount > $availableSum) {
$errorMsg .= "в банкомате недостаточно купюр";
} else {
$errorMsg = null;
}
return $errorMsg;
}
/*View results*/
function viewer($amount, $arr)
{
print "Сумма: $amount";
print "\n";
if (checker($amount, $arr) !== null) {
print "Выдача невозможна:" . checker($amount, $arr);
} else {
print "Выдача возможна, число купюр:";
print "\n";
foreach (countResult($amount, $arr) as $key => $val) {
print $key . "x" . $val . "\t";
}
}
}
/*Testing*/
viewer($amount, $bills);
PD9waHAKCi8qR2l2ZW4gZGF0YSovCiRhbW91bnQgPSA2NjAwOwoKJGJpbGxzID0gYXJyYXkoCgkxMDAgCT0+IDIzLAoJMjAwCQk9PiAzLAoJNTAwIAk9PiAxLAoJMTAwMAk9PiAwLAoJMjAwMCAJPT4gNCwKCTUwMDAgCT0+IDEKKTsKCi8qQ2FsY3VsYXRlIHJlc3VsdGluZyBncmVlbmJhY2tzKi8KZnVuY3Rpb24gY291bnRSZXN1bHQoJGFtb3VudCwgJGFycikgCnsKCSRncmVlbmJhY2sJPSBhcnJheV9yZXZlcnNlKGFycmF5X2tleXMoJGFycikpOwoJJHJlc3VsdCAJPSBhcnJheSgpOyAKCWZvcmVhY2ggKCRncmVlbmJhY2sgYXMgJGl0ZW0pIHsKCQkkY291bnQJPSAwOwoJCSR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