fork download
  1. <?php
  2.  
  3.  
  4. $bills = [500, 100];
  5. $totalBills = [5, 5, 5, 5];
  6. $rndCombination = [1, 5, 5, 5];
  7.  
  8. krsort($rndCombination);
  9. print_r($rndCombination);
  10.  
  11. function nextCombination($total, $combination){
  12. if($total == $combination){
  13. return 'Максимальная комбинация.';
  14. }
  15. krsort($combination);
  16. foreach($combination as $key => &$value){
  17. if($value < $total[$key]){
  18. $value++;
  19. break;
  20. }
  21. else{
  22. $value = 0;
  23. if($combination[$key-1] < $total[$key-1]){
  24. $combination[$key-1]++;
  25. }
  26. else{
  27. $combination[$key-1] = 0;
  28. $combination[$key-2]++;
  29. }
  30. break;
  31. }
  32. }
  33. ksort($combination);
  34. return $combination;
  35. }
  36.  
  37. print_r(nextCombination($totalBills, $rndCombination));
Success #stdin #stdout 0.02s 25864KB
stdin
Standard input is empty
stdout
Array
(
    [3] => 5
    [2] => 5
    [1] => 5
    [0] => 1
)
Array
(
    [0] => 1
    [1] => 6
    [2] => 0
    [3] => 0
)