Storytelling!
The Joseph Ring problem originated from a Jewish story.:
The Romans seized Bridge Tabat and 41 people hid in a cave to avoid the catastrophe.Among the 41 people included the historian Josephus and one of his friends.The remaining 39 decided to commit suicide collectively in order to show that they would not yield to the Romans.We have worked out a suicide program. All 41 people form a circle and the first person starts reporting the number clockwise. Every three people report suicide immediately, and then the next person starts reporting the number again. Still, every three people report suicide immediately...Until everyone commits suicide.
Joseph and his friends did not want to commit suicide, so Joseph thought of a strategy in which they both participated in the suicide program, but eventually avoided it.How did they do that, please?
There are many ways to analyze this question. My way of thinking is divided into two steps
The first step is to abstract a queue to add elements to another queue
Assuming there are n elements in Queue A, Queue A keeps on queuing. Each time an element is queued, it judges whether it meets the criteria for adding to Queue B (in conjunction with the Joseph Ring problem, if it is a multiple of 3, it does not meet the criteria for queuing).
Step 2 Improve Step 1
It can be thought of as a team S, which constantly performs the exit and entry operations (that is, the elements that leave the team are added to the end of the team again) and kicks if the elements that leave the team do not meet the criteria for re-entry.
The correlation function range() count() array_shift() array_push()
code implementation
function ysf($n,$m) { $i=0; $arr = range(1,$n);//Create: Array with 100 values while(count($arr)>1) { ++$i; //Keep going (that is, delete the first element of the array) $survice = array_shift($arr); if($i%$m<>0){ //Combining the Joseph Ring Problem //Report suicide on 3 (kick from queue) or wait for next trial (add to end of queue again) array_push($arr, $survice); } elseif($i>count($survice)){$i=0;}#This code doesn't matter at all, it's easy to understand } return $arr; } print_r( ysf(41,3) ); ######console################ Array ( [0] => 16 [1] => 31 ) //The reason for encapsulating a function here is simple: I can call it to escape death when I need it (Hahaha....)
Summarizing this method is not too efficient. It's a little expensive to operate...
function jesph(int $n,int $m){ $arr=range(1,$n); print_r($arr); $count=$n; $k=1;$j=1; $unset=[]; while($count>1){ while(in_array($j,$unset)){ $j++; if($j>$n) $j=1; continue 2; } if($k==$m){ unset($arr[$j]); $unset[]=$j; $count=count($arr); $k=1; }else{ $k++; } $j++; if($j>$n) $j=1; } echo current($arr); } jesph(10,3); function killMonkey($monkeys , $m , $current = 0){ $number = count($monkeys); $num = 1; if(count($monkeys) == 1){ echo $monkeys[0]."Monkey King"; return; } else{ while($num++ < $m){ $current++ ; $current = $current%$number; } echo $monkeys[$current]."The monkey was kicked off\n"; array_splice($monkeys , $current , 1); killMonkey($monkeys , $m , $current); } } $monkeys = range(1,41);//Array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); / /monkeys number $m = 3; //Number one monkey kicked killMonkey($monkeys , $m); function ysf($n,$m) { $i=0; $arr = range(1,$n);//Create: Array with 100 values while(count($arr)>1) { ++$i; //Keep going (that is, delete the first element of the array) $survice = array_shift($arr); if($i%$m<>0){ //Combining the Joseph Ring Problem //Report suicide on 3 (kick from queue) or wait for next trial (add to end of queue again) array_push($arr, $survice); } elseif($i>count($survice)){$i=0;}#This code doesn't matter at all, it's easy to understand } return $arr; } print_r( ysf(41,3) );
https://www.cnblogs.com/china90/p/7367396.html
#include <stdio.h> int f(int n, int m) { int s = 0; for (int i = 2; i <= n; i++) s = (s + m) % i; return s; } void main(int agrc,char *argv[]){ printf("dasfd ====== %d",f(41,3)); }
https://www.cnblogs.com/z-sm/p/4257092.html