时间:2021-07-01 10:21:17 帮助过:3人阅读
猴子一群,都带着号码的,站好了一圈,数到m的枪毙,剩下的接着数。如此往复,死剩下的一个就疯了
代码如下:
function killMonkeys($monkeys, $m){
$k = $m;
while (count($monkeys)){
$k = $k - 1;
$monkey = array_shift($monkeys);
if ($k) {
$monkeys[] = $monkey;
} else {
echo "" . $monkey . "号猴子毙了!
";
$k = $m;
}
}
echo $monkey . "号猴子斯巴达了";
}
$monkeys = range(0, 25);
unset($monkeys['0']);
$m = 5;
killMonkeys($monkeys, $m);