this article describes the monkey king algorithm implemented by PHP.

 <? PHP function getKingMokey ($n, $m) {$monkey[0] = 0; / / / / / / 1-N only monkey is numbered in the array for ($i= 1; = = = {} = {}); / / / loop traversal array element ( Monkey number) for ($i= 0; $i< $len; $i= $i) {$num = 0; foreach ($monkey as $key => {= = = 0); Ring if ($num = = 1) {echo $values; exit;} / / / / / / / / / $i only monkey kicked out of the team (the corresponding array location element value is set to 0) $monkey[$i] = 0; / / / / / / print the monkey's position echo $i. ""; / / / / / set the counter for ($j= 1; $j< = =; =). {/ / / monkey number plus one, traversing the next monkey $i++; / / / / if the monkey is not kicked out of the team, get the next monkey number if ($monkey[$i] > 0) continue; / / / if the element is 0, the monkey has been kicked out of the team and then circulate the next monkey number if ( $monkey[$i] = = = 0) {/ / / / / / take the next monkey number for ($k= $i; $k< $len; $k++) {/ / / / value is 0, numbered plus 1 if ($monkey[$k] = = 0) $i++; / / / / otherwise, the number has been obtained, and $monke is if ($monke) Y[$k] > 0) break;}} / / / if the number is larger than the number of monkeys, then the zeroth monkeys start traversing (array pointer to zero) / / step same if ($i = = $len) $i = 0; / / / same step, get the next monkey numbered if ($monkey[$i] = = 0) { For ($k= $i; $k< $len; $k++) {if ($monkey[$k] = = = 0) $i++; if ($monkey[$k] > 0),}}}} = 10; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / N, $m); > 

running results:

036927185104

with recursive algorithm

 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Ys, $m, $current = 0) {$number = count ($monkeys); $num = 1; if (count ($monkeys) = = 1) {echo $monkeys[0]. as "Monkey King";} Nt]. "the monkey is kicked off < br/>"; array_splice ($monkeys, $current, 1); killMonkey ($monkeys, $m, $current),} killMonkey ($monkeys,) The monkeys were kicked off 
3 and kicked off
10 monkeys were kicked off
9, monkeys were kicked off
1, monkeys were kicked off
6, and monkeys were kicked off
5 to be the monkey king,

more interested in PHP related content to see the site theme: , php program design algorithm summary ", "php string (string) Usage Summary " Rget= "_blank" >PHP mathematical operation skills summary "

"

hope that this article will help everyone PHP programming.

you may be interested in:


This concludes the body part

This paper fixed link:http://www.script-home.com/an-example-of-the-php-implementation-of-the-monkey-king-algorithm-monkey-king.html | Script Home | +Copy Link

Article reprint please specify:An example of the PHP implementation of the monkey king algorithm (Monkey King) | Script Home

You may also be interested in these articles!