PHP Implementation Joseph Summary of the method of the problem, PHP implementation
This paper summarizes the methods of implementing the Josephson problem in PHP. Share to everyone for your reference. The specific analysis is as follows:
A group of monkeys in a circle, according to the,..., n sequentially numbered. Then, starting from the 1th, Count to M., Kick It Out of the loop, start counting from behind it, Count to M., and Kick It out ... and so on, until the last monkey is left, the monkey is called the King. Requires programming to simulate this process, enter M, N, and output the number of the last king.
Analytical:
Joseph Ring is a mathematical application problem: known n individuals (denoted by number 1,2,3...N respectively) sit around a round table. From the person numbered K began to count, the number to m of the person out of the man, and his next person from 1 began to count, the number to m of the person again, according to this rule repeat until the round table all the people out.
Method One:
<?phpfunction Getleader ($n, $m) { $res =0; for ($i =2; $i <= $n; $i + +) { $res = ($res + $m)% $i; } return $res +1; } $leader = Getleader (13,34); Echo $leader;? >
Method Two:
<?php//defines function getking ($monkeys, $m, $current = 0) { $number = count ($monkeys); $num = 1; if (count ($monkeys) = = 1) { echo ' is numbered '. $monkeys [0]. ' Monkey became Monkey King! '; return; } else{ while ($num + + < $m) { $current + +; $current = $current% $number; } echo "is numbered". $monkeys [$current]. " The monkeys were kicked off ...
"; Array_splice ($monkeys, $current, 1); Getking ($monkeys, $m, $current); }} $n =13; Total number of monkeys $m =; The first few monkeys were kicked out. $monkeys = Range (1, $n); Put the monkey number in the array getking ($monkeys, $m); Call Function?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/972649.html www.bkjia.com true http://www.bkjia.com/PHPjc/972649.html techarticle PHP Implementation of the Josephson problem method summary, PHP Implementation Joseph Summary This paper summarizes the method of implementing the Josephson problem in PHP. Share to everyone for your reference. The specific analysis is as follows: one ...