fork download
  1. <?php
  2. // Staring straight up into the sky ... oh my my
  3.  
  4. function calculateDelivery(&$banknotes, $amount){
  5. ksort($banknotes);
  6. $amountSave = $amount;
  7. $INF = 1000000000;
  8. $minBanknotes = array();
  9. $minBanknotes[0][0] = 0;
  10.  
  11. for($m=0; $m<=$amount; $m++){
  12. $minBanknotes[0][$m] = $m==0?0:$INF;
  13. $p=1;
  14. foreach ($banknotes as $key => $value) {
  15. if($m <= $key-1){
  16. $minBanknotes[$p][$m] = $minBanknotes[$p-1][$m];
  17. }else{
  18. $minBanknotes[$p][$m] = min($minBanknotes[$p-1][$m], $minBanknotes[$p][$m-$key] + 1);
  19. }
  20. $p++;
  21. }
  22. }
  23.  
  24. $i = count($banknotes);
  25. $result = array_fill_keys(array_keys($banknotes), 0);
  26. $amount = $amountSave;
  27. krsort($banknotes, SORT_NUMERIC);
  28. while($amount>0){
  29. foreach ($banknotes as $key => $value) {
  30. while((($result[$key]+1) <= $banknotes[$key]) & ($amount-$key)>=0){
  31. if ($minBanknotes[$i][$amount-$key] == $minBanknotes[$i][$amount]-1){
  32. $result[$key]++;
  33. $amount -= $key;
  34. }else{
  35. echo $amount."\n";
  36. print_r($result);
  37. exit("Неверный путь");
  38. }
  39. }
  40. $i--;
  41. if($i<0){
  42. exit("Все пошло по пизде");
  43. }
  44. }
  45. }
  46.  
  47. $resultStr = "";
  48. foreach ($result as $key => $value) {
  49. if($value != 0){
  50. $resultStr = $resultStr."{$value}x{$key} ";
  51. }
  52. }
  53.  
  54. echo $resultStr."\n";
  55. }
  56.  
  57.  
  58. $amount = 34;
  59.  
  60. $banknotes = array (
  61. 2 => 3,
  62. 5 => 10,
  63. 10 => 1
  64. );
  65.  
  66. echo "Сумма: {$amount}\n";
  67. $rez = calculateDelivery($banknotes, $amount);
  68. ?>
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
Сумма: 34
2x2 4x5 1x10