"Joseph Ring" is a mathematical application problem: 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.
The following is a list of three ways to use PHP to solve this problem:
De-logically and sequentially remove
Recursive algorithm
Linear table Applications
Method one, in order to remove the logic in turn
function Getkingmokey ($n, $m) {$monkey [0] = 0; The 1-n only monkeys are numbered sequentially into the array for ($i = 1; $i <= $n; $i + +) {$monkey [$i] = $i; } $len = count ($monkey); Loop through the array element (monkey number) for ($i = 0; $i < $len; $i = $i) {$num = 0; /* Iterate through the $monkey array, calculate the number of elements in the array (number of remaining monkeys) * Assign to $num, and get the element value of the element with a value not 0 */foreach ($monkeyas $ke y = $value) {if ($value = = 0) continue; $num + +; $values = $value; }//If only one monkey is left output the monkey number (array element value) and exit the loop if ($num = = 1) {return$values; Exit }/* * If the remaining monkeys are greater than 1 ($num > 1) * Continue the program * *//$i The first monkey out of the team (the corresponding array position element value is set to 0) $ monkey[$i] = 0; /* Get the next monkey number that needs to be kicked out of the team * Walk the monkey in the range of $m values and set the counter of the $m * Remove a monkey number in turn * If the element value is 0, the position of the monkey has been kicked out of the team * If not 0, continue to get the next monkey number, and the counter plus 1 * If the number of monkeys obtained is greater than the number of arrays * then from the No. 0 monkeyThe child begins the traversal (array pointer zeroing) step above * until the counter reaches the $m value * The last obtained $i value is the next monkey number to kick out the team *//Set counter for ($j = 1; $j <= $m; $j + +) {//Monkey number plus one, traverse the next monkey $i + +; If the monkey is not kicked out of the squad, get the next monkey number if ($monkey [$i] > 0) continue; If the value of the element is 0, the monkey has been kicked out of the queue, which in turn takes down a monkey number if ($monkey [$i] = = 0) {//Remove a monkey number for ($k = $i; $k < $len; $k + +) {//value is 0, number plus 1 if ($monkey [$k] = = 0) $i + +; Otherwise, the number has been obtained, exit if ($monkey [$k] > 0) break; }}//If the number is greater than the number of monkeys, then the No. 0 Monkey starts traversing (array pointer zeroing) step above if ($i = = $len) $i = 0; Above step, get the next monkey number if ($monkey [$i] = = 0) {for ($k = $i; $k < $len; $k + +) {if ($monkey [$k] = = 0) $i + +; if ($monkey [$k] > 0) break; }}}}}//monkey number $n = 10; The number interval value of the kick-out team $m = 3; Call the Monkey King to get the function echo Getkingmokey ($n, $m). " is the Monkey King "; Method two, recursive algorithm [PHP] View plain copyfunction killmonkey ($monkeys, $m, $current = 0) {$number = count ($monkeys); $num = 1; if (count ($monkeys) = = 1) {echo$monkeys[0]. " Become the Monkey King "; Return } else{while ($num + + < $m) {$current + +; $current = $current% $number; } echo$monkeys[$current]. " The monkeys were kicked off the <br/> "; Array_splice ($monkeys, $current, 1); Killmonkey ($monkeys, $m, $current); }} $monkeys = Array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Monkeys's number $m = 3; Several monkeys were kicked out of the Killmonkey ($monkeys, $m);
method Three, the linear table applies
-
function yuesefu ($n, $m) {$r = 0; for ($i =2; $i <= $n; $i + +) {$r = ($r + $m)% $i; } return$r+1; } Echo Yuesefu (10,3). "
is the Monkey King ";