fork(1) 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. $error = '';
  22.  
  23. if ($input < 0) {
  24. $error = 'Сумма меньше нуля';
  25. } elseif (($input % 100) != 0) {
  26. $error = 'Число не кратно ста';
  27. } else {
  28. foreach ($bills as $bill => $count) {
  29. $issued[$bill] = min(floor($money / $bill), $count);
  30. $money -= ($issued[$bill] * $bill);
  31. }
  32.  
  33. if ($money != 0) {
  34. $error = 'Недостаточно купюр';
  35. }
  36.  
  37. $billsCount = '';
  38. $issued = array_reverse($issued, true);
  39. foreach ($issued as $bill => $count) {
  40. if ($count != 0) {
  41. $billsCount .= "{$bill}x{$count} ";
  42. }
  43. }
  44. }
  45.  
  46. echo "Сумма: {$input}\n";
  47. if ($error != '') {
  48. echo "Выдача невозможна: {$error}\n";
  49. } else {
  50. echo "Выдача возможна, число купюр:\n{$billsCount}";
  51. }
  52.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Сумма: 54500
Выдача возможна, число купюр:
5000x10 500x5 100x20