猴子一群,都带着号码的,站好了一圈,数到m的枪毙,剩下的接着数。如此往复,死剩下的一个就疯了 PHP ?phpfunction killMonkeys($monkeys, $m){ $k = $m; while (count($monkeys)){ $k = $k - 1; $monkey = array_shift($monkeys); if ($k) { $monkeys[] = $
猴子一群,都带着号码的,站好了一圈,数到m的枪毙,剩下的接着数。如此往复,死剩下的一个就疯了 PHP
<?php function killMonkeys($monkeys, $m){ $k = $m; while (count($monkeys)){ $k = $k - 1; $monkey = array_shift($monkeys); if ($k) { $monkeys[] = $monkey; } else { echo "<font color='red'>" . $monkey . "</font>号猴子毙了!<br/>"; $k = $m; } } echo $monkey . "号猴子斯巴达了"; } $monkeys = range(0, 25); unset($monkeys['0']); $m = 5; killMonkeys($monkeys, $m);