fork(1) download
  1. <?php
  2.  
  3. header('Content-Type: text/plain; charset=UTF-8');
  4.  
  5. $amount = 238800;
  6.  
  7. $bills = array(
  8. 5000 => 50,
  9. 1000 => 100,
  10. 500 => 100,
  11. 100 => 100
  12. );
  13.  
  14. /* Считаем количество денег в банкомате */
  15. function getBillSum($bills)
  16. {
  17. $BillSum = '';
  18.  
  19. foreach ($bills as $billValue => $billAmount) {
  20. $BillSum += $billValue * $billAmount;
  21. }
  22.  
  23. return $BillSum;
  24. }
  25.  
  26. /* Проверяем возможность выдачи */
  27. function checkPaymentPossibility($amount, $billSum)
  28. {
  29. $error = '';
  30.  
  31. if ($billSum < $amount) {
  32. $error = "в банкомате недостаточно купюр";
  33. }
  34.  
  35. if ($amount <= 0) {
  36. $error = "сумма должна быть больше нуля";
  37. }
  38.  
  39. if (($amount % 100) != 0) {
  40. $error = "сумма должна быть кратна ста";
  41. }
  42.  
  43. if (!empty($error)) {
  44. return "Выдача невозможна: $error";
  45. }
  46.  
  47. return TRUE;
  48. }
  49.  
  50. function giveCash($amount, $bills)
  51. {
  52. $result = '';
  53.  
  54. foreach ($bills as $billValue => $billAmount) {
  55. $billPaymentAmount = floor($amount / $billValue);
  56.  
  57. if ($billPaymentAmount > $billAmount) {
  58. $billPaymentAmount = $billAmount;
  59. }
  60.  
  61. $amount -= $billPaymentAmount * $billValue;
  62.  
  63. $result .= "{$billPaymentAmount}x{$billValue} ";
  64. }
  65.  
  66. return "$result";
  67. }
  68.  
  69. function processATMRequest ($amount, $bills)
  70. {
  71. $result = '';
  72. $billSum = getBillSum($bills);
  73.  
  74. $errors = checkPaymentPossibility($amount, $billSum);
  75.  
  76. if ($errors !== TRUE) {
  77. return $errors;
  78. }
  79.  
  80. $cash = giveCash($amount, $bills);
  81.  
  82. $result .= "Сумма: $amount\n";
  83. $result .= "Выдача возможна, число купюр: $cash\n";
  84.  
  85. return $result;
  86.  
  87. }
  88.  
  89. echo processATMRequest($amount, $bills);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Сумма: 238800
Выдача возможна, число купюр: 47x5000 3x1000 1x500 3x100