fork download
  1. <?php
  2.  
  3.  
  4. $total = 7;
  5. $step = 4;
  6.  
  7. $citizens = range(1, $total);
  8. $positions = array_combine($citizens, $citizens);
  9. print_r($positions);
  10.  
  11. while (count($positions) >= $step) {
  12.  
  13. for ($i = 1; $i < $step; $i++) {
  14. $cur = next($positions);
  15.  
  16. if ($cur === false) {
  17. echo "the $i iteration - reached the end. Rewinding...\n";
  18. $cur = reset($positions);
  19. echo "now current is ".current($positions)."\n";
  20.  
  21. }
  22. }
  23. echo "deleting $cur. \n";
  24. unset($positions[$cur]);
  25.  
  26. if (current($positions) === false) {
  27. reset($positions);
  28. }
  29.  
  30. echo "count will now start from " . current($positions) . "\n";
  31. echo "The people still in game:\n";
  32. print_r($positions);
  33. }
  34. echo "Выигрышные места: ";
  35. echo implode(", ", array_values($positions)) . ". \n";
  36.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
  Array
(
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
)
deleting 4. 
count will now start from 5
The people still in game:
Array
(
    [1] => 1
    [2] => 2
    [3] => 3
    [5] => 5
    [6] => 6
    [7] => 7
)
the 3 iteration - reached the end. Rewinding...
now current is 1
deleting 1. 
count will now start from 2
The people still in game:
Array
(
    [2] => 2
    [3] => 3
    [5] => 5
    [6] => 6
    [7] => 7
)
deleting 6. 
count will now start from 7
The people still in game:
Array
(
    [2] => 2
    [3] => 3
    [5] => 5
    [7] => 7
)
the 1 iteration - reached the end. Rewinding...
now current is 2
deleting 5. 
count will now start from 7
The people still in game:
Array
(
    [2] => 2
    [3] => 3
    [7] => 7
)
Выигрышные места: 2, 3, 7.