Home >php教程 >php手册 >猴子选大王,猴子大王

猴子选大王,猴子大王

WBOY
WBOYOriginal
2016-06-13 08:39:541154browse

猴子选大王,猴子大王

 一群猴子排成一圈,按1,2,…,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入m、n, 输出最后那个大王的编号

 

// 基本思路,判断猴子是否为出局的,是就删除,不是就放在数据尾部
function xdw($m, $n){
for($i = 1; $i $arr[] = $i;
}

for($i = 0; count($arr)>1; $i++){
if( ($i + 1) % $m == 0){
unset($arr[$i]);
}else{
array_push($arr, $arr[$i]);
unset($arr[$i]);
}
}
return $arr;
}
print_r(xdw(2,10));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn