fork download
  1. <?php
  2.  
  3. header('Content-Type: text/plain; charset=UTF-8');
  4.  
  5. $amount = 1600;
  6.  
  7. $bills = array(
  8. 5000 => 0,
  9. 2000 => 0,
  10. 1000 => 1,
  11. 500 => 100,
  12. 200 => 4,
  13. 100 => 0
  14. );
  15.  
  16. /* Считаем количество денег в банкомате */
  17. function getBillSum($bills)
  18. {
  19. $BillSum = '';
  20.  
  21. foreach ($bills as $billValue => $billAmount) {
  22. $BillSum += $billValue * $billAmount;
  23. }
  24.  
  25. return $BillSum;
  26. }
  27.  
  28. /* Проверяем возможность выдачи */
  29. function checkPaymentPossibility($amount, $bills)
  30. {
  31. $error = '';
  32.  
  33. $billSum = getBillSum($bills);
  34.  
  35. if ($billSum < $amount) {
  36. $error = "в банкомате недостаточно купюр";
  37. }
  38.  
  39. if ($amount <= 0) {
  40. $error = "сумма должна быть больше нуля";
  41. }
  42.  
  43. if (($amount % 100) != 0) {
  44. $error = "сумма должна быть кратна ста";
  45. }
  46.  
  47. if (!empty($error)) {
  48. return "Выдача невозможна: $error";
  49. }
  50.  
  51. return TRUE;
  52. }
  53.  
  54. function giveCash($amount, $bills, $billUsed=NULL, $tempResult=NULL)
  55. {
  56. $result = '';
  57. $k = 0;
  58. $billKeys = array();
  59.  
  60. foreach ($bills as $billValue => $billAmount) {
  61. if ($billValue > $amount || $billAmount == 0 || !empty($billUsed[$billValue])) {
  62. continue;
  63. }
  64.  
  65. $billPaymentAmount = floor($amount / $billValue);
  66.  
  67. if ($billPaymentAmount == 0) {
  68. continue;
  69. }
  70.  
  71. if ($billPaymentAmount > $billAmount) {
  72. $billPaymentAmount = $billAmount;
  73. }
  74.  
  75. $amount -= $billPaymentAmount * $billValue;
  76.  
  77. $billUsed[$billValue] = TRUE;
  78.  
  79. $billKeys[] = $billValue;
  80. $k++;
  81.  
  82. $tempResult[$billValue] = $billPaymentAmount;
  83. }
  84.  
  85. if ($amount != 0) {
  86. $l = $k - 1;
  87. $tempResult[$billKeys[$l]] -= 1;
  88. $amount += $billKeys[$l];
  89. giveCash($amount, $bills, $billUsed, $tempResult);
  90. }
  91.  
  92. var_dump($tempResult);
  93.  
  94. foreach ($tempResult as $billValue => $billAmount) {
  95. $result .= "{$billAmount}x$billValue ";
  96. }
  97.  
  98. return $result;
  99. }
  100.  
  101. function processATMRequest ($amount, $bills)
  102. {
  103. $result = '';
  104.  
  105. $errors = checkPaymentPossibility($amount, $bills);
  106.  
  107. if ($errors !== TRUE) {
  108. return $errors;
  109. }
  110.  
  111. $cash = giveCash($amount, $bills);
  112.  
  113. $result .= "Сумма: $amount\n";
  114. $result .= "Выдача возможна, число купюр: $cash\n";
  115.  
  116. return $result;
  117.  
  118. }
  119.  
  120. echo processATMRequest($amount, $bills);
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
array(3) {
  [1000]=>
  float(1)
  [500]=>
  float(0)
  [200]=>
  float(3)
}
array(2) {
  [1000]=>
  float(1)
  [500]=>
  float(0)
}
Сумма: 1600
Выдача возможна, число купюр: 1x1000 0x500