[题目]
[解析]
方法一:
<?php
function getLeader($n,$m) {
$res=0;
for($i=2; $i<=$n; $i++) {
$res=($res+$m)%$i;
}
return $res+1;
}
$leader = getLeader(13,34);
echo $leader;
?>
方法二:
<?php
//定义函数
function getKing($monkeys , $m , $current = 0){
$number = count($monkeys);
$num = 1;
if(count($monkeys) == 1){
echo '<font color="red">编号为'.$monkeys[0].'的猴子成为猴王了!</font>';
return;
}else{
while($num++ < $m){
$current++ ;
$current = $current%$number;
}
echo "编号为".$monkeys[$current]."的猴子被踢掉了...<br/>";
array_splice($monkeys , $current , 1);
getKing($monkeys , $m , $current);
}
}
$n=13; //总共猴子数目
$m = 34; //数到第几只的那只猴子被踢出去
$monkeys = range(1,$n); //将猴子编号放入数组中
getKing($monkeys , $m); //调用函数
?>