Home  >  Article  >  Backend Development  >  Detailed explanation of how to implement the Joseph Ring Problem in PHP

Detailed explanation of how to implement the Joseph Ring Problem in PHP

jacklove
jackloveOriginal
2018-07-06 17:53:171896browse

This article mainly introduces the method of realizing the Joseph ring problem in PHP, and analyzes the related operating skills of PHP using loops and recursion to realize the Joseph ring problem in the form of examples. Friends in need can refer to the following

The examples of this article are described A method to implement the Joseph ring problem in PHP. Share it with everyone for your reference, the details are as follows:

1. Overview

Let’s first take a look at the more common descriptions of Joseph ring problems on the Internet: The Joseph Ring (Joseph problem) is a mathematical application problem: it is known that n people (represented by numbers 1, 2, 3...n respectively) are sitting around a round table. Start counting from the person numbered k, and the person who counts to m comes out of the queue; the next person starts counting from 1, and the person who counts to m comes out of the queue again; repeat this pattern until around the round table All the people came out. Usually when solving this kind of problem, we number it from 0 to n-1, and the final result 1 is the solution to the original problem.

2. Implementation code

1. Loop

function circle($arr,$idx,$k){
  for($i=0;$i<$idx;$i++){
    $tmp = array_shift($arr);
    array_push($arr,$tmp);
  }
  $j = 1;
  while(count($arr) > 0){
    $tmp = array_shift($arr);
    if($j++%$k == 0){
      echo $tmp."\n";
    }else{
      array_push($arr,$tmp);
    }
  }
}
$arr = array(1,2,3,4,5,6,7,8,9,10,11,12);
$idx = 3;
$k = 4;
circle($arr,$idx,$k);

Run result:

7 11 3 8 1 6 2 10 9 12 5 4

2. Recursion

function circle($arr,$idx,$k){
  $len = count($arr);
  $i = 1;
  if($len == 1){
    echo $arr[0]."\n";
    return ;
  } else {
    while($i++ < $k){
      $idx++;
      $idx = $idx%$len;
    }
    echo $arr[$idx]."\n";
    array_splice($arr,$idx,1);
    circle($arr,$idx,$k);
  }
}
$arr = [1,2,3,4,5,6,7,8,9,10,11,12];
$idx = 3;
$k = 4;
circle($arr,$idx,$k);

Run results:

7 11 3 8 1 6 2 10 9 12 5 4

##Articles you may be interested in:

An explanation of the method of using Passport to implement Auth authentication in Laravel5.5

Performance optimization tool you may overlook in PHP: Generator-related content

Detailed implementation of composer automatic loading in the Laravel framework

The above is the detailed content of Detailed explanation of how to implement the Joseph Ring Problem in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn