Monkey King Algorithm implemented in PHP (monkey chooses the king)

不言
Release: 2023-03-25 07:54:01
Original
4691 people have browsed it

This article mainly introduces the Monkey King algorithm (monkey chooses the king) implemented in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

The examples in this article are described Monkey King algorithm implemented in PHP. Share it with everyone for your reference, the details are as follows:

<?php
function getKingMokey($n, $m)
{
    $monkey[0] = 0;
    //将1-n只猴子顺序编号 入数组中
    for($i= 1; $i<= $n; $i++)
    {
        $monkey[$i] = $i;
    }
    $len = count($monkey);
    //循环遍历数组元素(猴子编号)
    for($i= 0; $i< $len; $i= $i)
    {
       $num = 0;
       foreach($monkey as $key => $value)
       {
        if($value == 0) continue;
        $num++;
        $values = $value;
       }
       //若只剩一只猴子 则输出该猴子编号(数组元素值) 并退出循环
       if($num == 1)
       {
          echo $values;
          exit;
       }
       //将第$i只猴子踢出队伍(相应数组位置元素值设为0)
       $monkey[$i] = 0;
       //打印该猴子位置
       echo $i."";
       //设置计数器
       for($j= 1; $j<= $m; $j++)
       {
          //猴子编号加一,遍历下一只猴子
          $i++;
          //若该猴子未被踢出队伍,获取下一只猴子编号
          if($monkey[$i] > 0) continue;
          //若元素值为0,则猴子已被踢出队伍,进而循环取下一只猴子编号
          if($monkey[$i] == 0)
          {
              //取下一只猴子编号
              for($k= $i; $k< $len; $k++)
              {
                  //值为0,编号加1
                  if($monkey[$k] == 0) $i++;
                  //否则,编号已取得,退出
                  if($monkey[$k] > 0) break;
              }
          }
          //若编号大于猴子个数,则从第0只猴子开始遍历(数组指针归零)
          //步骤同上
          if($i == $len) $i = 0;
          //同上步骤,获取下一只猴子编号
          if($monkey[$i] == 0)
          {
             for($k= $i; $k< $len; $k++)
             {
                  if($monkey[$k] == 0) $i++;
                 if($monkey[$k] > 0) break;
             }
          }
      }
   }
}
//猴子个数
$n = 10;
//踢出队伍的编号间隔值
$m = 3;
//调用猴王获取函数
getKingMokey($n, $m);
?>
Copy after login

Running results:

##036927185104

Using recursive algorithm

$monkeys = array(1 , 2 , 3 , 4 , 5 , 6 , 7, 8 , 9 , 10); //monkey的编号
$m = 4; //数到第几只的那只猴子被踢出去
function killMonkey($monkeys , $m , $current = 0){
  $number = count($monkeys);
    $num = 1;
    if(count($monkeys) == 1){
      echo $monkeys[0]."成为猴王了";
      return;
    }
    else{
      while($num++ < $m){
          $current++ ;
          $current = $current%$number;
        }
        echo $monkeys[$current]."的猴子被踢掉了<br/>";
        array_splice($monkeys , $current , 1);
        killMonkey($monkeys , $m , $current);
    }
}
killMonkey($monkeys , $m);
Copy after login
Run result:

#4 monkey was kicked off

8 monkey was kicked off
2 monkey was kicked off
7 The monkey was kicked off
3 monkey was kicked off
10 monkey was kicked off
9 monkey was kicked off
1 monkey was kicked off
6 The monkey was kicked off
5 and became the Monkey King

Related recommendations:

PHP implementation of shopping website

Detailed explanation of how PHP implements the execution of external programs


The above is the detailed content of Monkey King Algorithm implemented in PHP (monkey chooses the king). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template