1 Star 0 Fork 0

平凡亦是收获/数据结构和算法

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
约瑟夫环问题v2.php 902 Bytes
Copy Edit Raw Blame History
liuzeming authored 2021-01-10 16:38 +08:00 . add
<?php
function king($m, $n)
{
//构建初始序列,记录m只猴子在圈中
$data = array_fill(0, $m, 1);
$t=0; // 报数位置,首次报数的起始位置为0,是第1只猴子
$s = 0; // 报数累计
for ($i=1; $i<=$m; ) // 循环要执行m次,有m个猴子要出圈
{
$s += $data[$t]; // s累加中,猴子在圈时加1相当于报数,出圈后是加0相当于没有报数
// 已经有n个未出圈猴子报数
if($s == $n)
{
$data[$t]=0; //猴子出圈
echo ($t+1).PHP_EOL; //输出出圈猴子的编号
$s = 0; //重新开始累加
$i++; //出圈的猴子加1
}
$t=($t+1) % $m; //再报数时,从下一只猴子开始
}
return $t+1;
}
king(8, 4);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lzmcode/data-structure-and-algorithm.git
git@gitee.com:lzmcode/data-structure-and-algorithm.git
lzmcode
data-structure-and-algorithm
数据结构和算法
master

Search