<?php
/*Given data*/
$amount = 6600;
100 => 0,
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+IDAsCgkyMDAJCT0+IDMsCgk1MDAgCT0+IDEsCgkxMDAwCT0+IDAsCgkyMDAwIAk9PiA0LAoJNTAwMCAJPT4gMQopOwoKLypDYWxjdWxhdGUgcmVzdWx0aW5nIGdyZWVuYmFja3MqLwpmdW5jdGlvbiBjb3VudFJlc3VsdCgkYW1vdW50LCAkYXJyKSAKewoJJGdyZWVuYmFjawk9IGFycmF5X3JldmVyc2UoYXJyYXlfa2V5cygkYXJyKSk7CgkkcmVzdWx0IAk9IGFycmF5KCk7IAoJZm9yZWFjaCAoJGdyZWVuYmFjayBhcyAkaXRlbSkgewoJCSRjb3VudAk9IDA7CgkJJH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