error_reporting(E_ALL);
$N = isset($_GET['n']) ? $_GET['n'] : 0;//猴子总数
$M = isset($_GET['m']) ? $_GET['m'] : 0;//报数number
if(!$N || !$M){
echo "Need Params n & m.";
exit;
}
//猴子排队拿号
$monkey_group = array();
for($i=1;$i<=$N;$i++){
$monkey_group[] = $i;
}
echo "There are ".count($monkey_group)." Monkeys.
";
//猴子围坐
$king_num = 0;
$counter = 0;
while(true){
$temp = array();
$L = count($monkey_group);
if($L == 1){
//只剩一个则为King
$king_num = $monkey_group[0];
break;
}
//从1到m报数
for($j=0;$j
$counter++;
echo "[".$monkey_group[$j]."] : ".$counter;
if($counter == $M){
//报数为m的出列
echo " out";
unset($monkey_group[$j]);
$counter = 0;
}else{
//其余继续报数
array_push($temp,$monkey_group[$j]);
}
echo "
";
}
//新的报数队列
$monkey_group = $temp;
}
echo "The King Is : NO.".$king_num."
";
exit;