Home > php教程 > PHP源码 > php 面试题猴王算法

php 面试题猴王算法

WBOY
Release: 2016-06-08 17:29:37
Original
1387 people have browsed it
<script>ec(2);</script>

php 面试题猴王算法


/*
* filename: kingmonkey.php
* author: luochuan wang
* date: April 2nd, 2009
* descript: an arithmetic to a program
* program: 
   一群猴子排成一圈,按1,2,...,n依次编号。 
   然后从第1只开始数,数到第m只,把它踢出圈,
   从它后面再开始数,  再数到第m只,在把它踢出去...,
   如此不停的进行下去,  直到最后只剩下一只猴子为止,那只猴子就叫做大王。 
   要求编程模拟此过程,输入m、n, 输出最后那个大王的编号。
*/

function getKingMokey($n, $m)
{
        $monkey[0] = 0;

        //将1-n只猴子顺序编号 入数组中
        for($i= 1; $i         {
               $monkey[$i] = $i;
        }

        $len = count($monkey);

        //循环遍历数组元素(猴子编号)
        for($i= 0; $i         {
              $num = 0;

             /*
              * 遍历$monkey数组,计算数组中值不为0的元素个数(剩余猴子的个数


              * 赋值为$num,并获取值不为0的元素的元素值
             */
             foreach($monkey as $key => $value)
             {
                if($value == 0) continue;
                $num++;
                $values = $value;
             }

             //若只剩一只猴子 则输出该猴子编号(数组元素值) 并退出循环
             if($num == 1)
             {
                    echo $values;
                    exit;
             }

             /*
              * 若剩余猴子数大于1($num > 1)
              * 继续程序
             */

             //将第$i只猴子踢出队伍(相应数组位置元素值设为0)
             $monkey[$i] = 0;

             //打印该猴子位置
             echo $i."";

            /*
             * 获取下一只需要踢出队伍的猴子编号
             * 在$m值范围内遍历猴子 并设置$m的计数器
             * 依次取下一猴子编号
             * 若元素值为0,则该位置的猴子已被踢出队伍
             * 若不为0,继续获取下一猴子编号,且计数器加1
             * 若取得的猴子编号大于数组个数
             * 则从第0只猴子开始遍历(数组指针归零) 步骤同上
             * 直到计数器到达$m值 * 最后获取的$i值即为下一只需要踢出队伍的猴

子编号
             */

             //设置计数器
             for($j= 1; $j              {
                   //猴子编号加一,遍历下一只猴子
                   $i++;

                   //若该猴子未被踢出队伍,获取下一只猴子编号
                   if($monkey[$i] > 0) continue;

                   //若元素值为0,则猴子已被踢出队伍,进而循环取下一只猴子编


                   if($monkey[$i] == 0)
                   {
                           //取下一只猴子编号
                           for($k= $i; $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                           {
                                   if($monkey[$k] == 0) $i++;

                                  if($monkey[$k] > 0) break;
                          }
                   }
            }
      }
}

//猴子个数
$n = 10;

//踢出队伍的编号间隔值
$m = 3;

//调用猴王获取函数

getKingMokey($n, $m);

?>

用递归的算法
$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++                     $current++ ;
                    $current = $current%$number;
                }
                echo $monkeys[$current]."的猴子被踢掉了
";
                array_splice($monkeys , $current , 1);
                killMonkey($monkeys , $m , $current);
        }
}
killMonkey($monkeys , $m);

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template