fork download
  1. <?php
  2. $total=30; //number of people
  3. $skip=5; //couplets
  4. $key=$skip;
  5. $M=$skip; //stopper. once reached M exit loop
  6. $arrayOfStudents=range(1,$total,1);
  7. end($arrayOfStudents);
  8. $lastKey=key($arrayOfStudents);
  9. //last available student in each cycle. count($array) wont work since at the end of the cycle we have changed (decreased) array.length because of dropped students
  10. //but this must not affect last student in the cycle (determined in the beginning of each cycle) since countdown was not interrupted or renewed after each drop
  11.  
  12. while (count($arrayOfStudents)>=$M){
  13.  
  14.  
  15. if ($key-1<=$lastKey) {
  16. unset($arrayOfStudents[$key-1]);
  17. $key+=$skip;
  18.  
  19. }
  20.  
  21. elseif ($key-1>$lastKey) {
  22. $key=($key-1)-$lastKey;
  23. $arrayOfStudents=array_values($arrayOfStudents);
  24. end($arrayOfStudents);
  25. $lastKey=key($arrayOfStudents);
  26. unset($arrayOfStudents[$key-1]);
  27. $key+=$skip;
  28.  
  29. }
  30.  
  31.  
  32. }
  33.  
  34. echo "Выигрышные места: " . implode(", ",$arrayOfStudents);
  35.  
  36. ?>
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
Выигрышные места: 3, 4, 14, 27