Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexchent committed Dec 15, 2019
1 parent 962febf commit eecd96d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 0 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions suanfa/monkey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php/** * 一群猴子排成一圈,按1,2,…,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈, * 从它后面再开始数,再数到第m只,在把它踢出去…, * 如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大王。 * 要求编程模拟此过程,输入m、n, 输出最后那个大王的编号。用程序模拟该过程。 * Created by PhpStorm. * User: chentao * Date: 2019/12/15 * Time: 8:35 PM */function monkey(int $m,int $n):int{ $arr = range(1, $n, 1); //生成猴群数组 $i = 0;//当前指针 while(count($arr)>1) { if(($i+1)%$m === 0) { unset($arr[$i]); //数到m的猴子踢出去,保证数组的索引不变 } else { //不是m的猴子排到队尾 array_push($arr,$arr[$i]); unset($arr[$i]); } $i++; } return $arr[$i];}echo monkey(3,5);
Expand Down

0 comments on commit eecd96d

Please sign in to comment.