fork download
  1. <?php
  2. //Сколько выдать
  3. $input = 54500;
  4.  
  5. //Купюры в наличии
  6. $bills = array(
  7. 100 => 23,
  8. 500 => 5,
  9. 1000 => 0,
  10. 5000 => 200
  11. );
  12. $bills = array_reverse($bills, true);
  13. //Количество выданных купюр
  14. $issued = array(
  15. 100 => 0,
  16. 500 => 0,
  17. 1000 => 0,
  18. 5000 => 0
  19. );
  20. $money = $input;
  21.  
  22. if ($input < 0) {
  23. $error = 'Сумма меньше нуля';
  24. } elseif (($input % 100) != 0) {
  25. $error = 'Число не кратно ста';
  26. } else {
  27. foreach ($bills as $bill => $count) {
  28. if (($count > 0) && ($money >= $bill)) {
  29. for ($i = 0; $i < $count; $i++) {
  30. $money -= $bill;
  31. $issued[$bill]++;
  32. if ($money < $bill) {
  33. break;
  34. }
  35. }
  36. } else {
  37. continue;
  38. }
  39. }
  40.  
  41. if ($money != 0) {
  42. $error = 'Недостаточно купюр';
  43. }
  44.  
  45. $billsCount = '';
  46. $issued = array_reverse($issued, true);
  47. foreach ($issued as $bill => $count) {
  48. if ($count != 0) {
  49. $billsCount .= "{$bill}x{$count} ";
  50. }
  51. }
  52. }
  53.  
  54. echo "Сумма: {$input}\n";
  55. if (isset($error)) {
  56. echo "Выдача невозможна: {$error}\n";
  57. } else {
  58. echo "Выдача возможна, число купюр:\n{$billsCount}";
  59. }
  60.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Сумма: 54500
Выдача возможна, число купюр:
5000x10 500x5 100x20