约瑟夫问题(猴子选大王)

版权声明:转发原创文章请复制文章链接地址 https://blog.csdn.net/weixin_42579642/article/details/85859728
/**
 * @param $n
 * @param $m
 * @return array
 * 猴子选大王
 */
function choose($n,$m){
    for($i=1;$i<=$n;$i++){
        $arr[$i] = $i;
    }
    $tmp = 1;
    while(count($arr)>1){
        if($tmp % $m != 0){
            $arr[] = $arr[$tmp];
        }
        unset($arr[$tmp]);
        $tmp++;
    }
    return $arr;
}
print_r(choose(8,5));

猜你喜欢

转载自blog.csdn.net/weixin_42579642/article/details/85859728