The Monkey King algorithm for PHP questions

Source: Internet
Author: User

The Monkey King algorithm for PHP questions


<?php
/*
* filename:kingmonkey.php
* Author:luochuan Wang
* Date:april 2nd, 2009
* Descript:an arithmetic to a
* Program:
A group of monkeys in a circle, press 1,2,..., n sequentially numbered.
And then starting from the 1th, counting to the first m, kicking it out of the loop,
Start from behind it again, count to M only, kick It out ...,
Keep on going until the last monkey is left, the monkey is called the King.
Requires programming to simulate this process, input m, n, output the last king's number.
*/

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

Numbering 1-n monkeys sequentially in an array
for ($i = 1; $i <= $n; $i + +)
{
$monkey [$i] = $i;
}

$len = count ($monkey);

Looping through array elements (monkey numbers)
for ($i = 0; $i < $len; $i = $i)
{
$num = 0;

/*
* Iterate over the $monkey array to calculate the number of elements in the array with a value of not 0 (the number of remaining monkeys)

)
              * Assign to $num and get the element value of an element with a value of not 0
             */
              foreach ($monkey as $key => $value)
    & nbsp;        {
                 if ($value = 0) continue;
                $num + +;
                $values = $value;
            }

If only one monkey is left, the monkey number (array element value) is exported and the loop is exited
if ($num = = 1)
{
Echo $values;
Exit
}

/*
* If the number of monkeys remaining is greater than 1 ($num > 1)
* Continue the program
*/

Kick the $i monkey out of the team (the corresponding array position element value is set to 0)
$monkey [$i] = 0;

Print the monkey position
echo $i. "";

/*
* Get the next monkey number that needs to be kicked out of the team
* Traverse the monkey in the $m value range and set the $m counter
* Take a monkey number in turn
* If the element value is 0, then the position of the monkey has been kicked out of the team
* If not 0, continue to get the next monkey number, and counter plus 1
* If the monkey number is greater than the number of arrays
* Then start traversing the No. 0 monkey (array pointer to zero) step above
* Until the counter reaches the $m value * The last $i value is the next monkey that needs to be kicked out of the team.

Sub number
*/

Set up counters
for ($j = 1; $j <= $m; $j + +)
{
Monkey number plus one, traversing the next monkey
$i + +;

If the monkey is not kicked out of the team, get the next monkey number
if ($monkey [$i] > 0) continue;

If the element value is 0, then the monkey has been kicked out of the team, and then cycle down a monkey

Resolution
if ($monkey [$i] = = 0)
{
Remove a monkey number
for ($k = $i; $k < $len; $k + +)
{
Value is 0, number plus 1.
if ($monkey [$k] = = 0) $i + +;

Otherwise, the number has been obtained, the exit
if ($monkey [$k] > 0) break;
}
}

If the number is larger than the monkeys, the No. 0 monkey begins to traverse (array pointer to zero)

Step ditto
                    if ($i = = $len) $i = 0;

                   //Previous steps, get the next monkey number
                    if ($monkey [$i] = = 0)
                    {
                           for ($k = $i; $k < $len; $k + +)
                           {
                                     if ($monkey [$k] = = 0) $i + +;

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

Number of monkeys
$n = 10;

Kick out the number interval value of the team
$m = 3;

Call Monkey Get function

Getkingmokey ($n, $m);

?>

A recursive algorithm
$monkeys = Array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Number of Monkey
$m = 4; The monkey was kicked out of the first few.
* Monkey King Algorithm * *
/* Process-oriented implementation * *
function Killmonkey ($monkeys, $m, $current = 0) {
$number = count ($monkeys);
$num = 1;
if (count ($monkeys) = 1) {
echo $monkeys [0]. " Become the Monkey King ";
Return
}
else{
while ($num + + < $m) {
$current + +;
$current = $current% $number;
}
echo $monkeys [$current]. " The monkeys were kicked off the <br/> ";
Array_splice ($monkeys, $current, 1);
Killmonkey ($monkeys, $m, $current);
}
}
Killmonkey ($monkeys, $m);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.