Monkey Choose King, Monkey King
A group of monkeys in a circle, according to 1,2,...,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, output the last king's number
The basic idea, judge whether the monkey is out, is to delete, not just put in the data tail
function Xdw ($m, $n) {
for ($i = 1; $i <= $n; $i + +) {
$arr [] = $i;
}
for ($i = 0; count ($arr) >1; $i + +) {
if (($i + 1)% $m = = 0) {
Unset ($arr [$i]);
}else{
Array_push ($arr, $arr [$i]);
Unset ($arr [$i]);
}
}
return $arr;
}
Print_r (XDW (2,10));
http://www.bkjia.com/PHPjc/1127370.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127370.html techarticle Monkey choose King, Monkey King a group of monkeys lined up in a circle, according to 1,2,,n sequentially numbered. Then, starting from the 1th, count to the number of M, kick it out of the loop, start counting from behind it, and count to ...