$monkeys = array(1 , 2 , 3 , 4 , 5 , 6 , 7, 8 , 9 , 10); //monkey的编号 $m = 4; //数到第几只的那只猴子被踢出去 /* 猴王算法*/ function killMonkey($monkeys, $m, $current = 0) { //当前是第几只 $num = 1; if (count($monkeys) == 1) { echo $monkeys[0]."是猴王"; return; } while ($num < $m) { $num ++; $current ++; $current = $current % count($monkeys); } if ($num == $m ) { // 踢出去第$num只猴子 echo $monkeys[$current]."的猴子被踢掉了<br/>"; array_splice($monkeys, $current, 1); killMonkey($monkeys, $m, $current); } } killMonkey($monkeys,$m);