Ec (2); php interview questions Monkey King Algorithm & lt ;? Php ** filename: kingmonkey. php * author: luochuanwang * date: April2nd, 2009 * descript: anarithmetictoaprogram * program: & nbsp; a group of monkeys are arranged in a circle, press 1, 2 ,..., script ec (2); script
Php interview questions Monkey King Algorithm
/*
* Filename: kingmonkey. php
* Author: luochuan wang
* Date: interval l 2nd, 2009
* Descript: an arithmetic to a program
* Program:
A group of monkeys are arranged in a circle and numbered by 1, 2,... and n.
Then start from 1st, count to m, and kick it out of the circle,
Start counting after it, count to m, and kick it out ...,
The monkey is called the King until there is only one monkey.
Programming to simulate this process, input m, n, and output the number of the last king.
*/
Function getKingMokey ($ n, $ m)
{
$ Monkey [0] = 0;
// Sequentially number 1-N monkeys into the Array
For ($ I = 1; $ I <= $ n; $ I ++)
{
$ Monkey [$ I] = $ I;
}
$ Len = count ($ monkey );
// Cyclically traverse the array element (monkey ID)
For ($ I = 0; $ I <$ len; $ I = $ I)
{
$ Num = 0;
/*
* Traverses the $ monkey array and calculates the number of elements whose values are not 0 (number of remaining monkeys ).
)
* Assign a value of $ num and obtain the element value of an element whose value is not 0.
*/
Foreach ($ monkey as $ key => $ value)
{
If ($ value = 0) continue;
$ Num ++;
$ Values = $ value;
}
// If only one monkey is left, output the Monkey number (array element value) and exit the loop.
If ($ num = 1)
{
Echo $ values;
Exit;
}
/*
* If the remaining number of monkeys is greater than 1 ($ num> 1)
* Continue the program
*/
// Kick the $ I monkey out of the team (the element value in the corresponding array is set to 0)
$ Monkey [$ I] = 0;
// Print the monkey position
Echo $ I ."";
/*
* Obtain the number of the monkey that needs to be kicked out of the team.
* Traverse the monkey in the range of $ m and set the counter of $ m.
* Sequentially remove a monkey number
* If the element value is 0, the monkey in the position has been kicked out of the team.
* If the value is not 0, continue to obtain the next monkey number and Add 1 to the counter.
* If the number of monkeys obtained is greater than the number of arrays
* Traverse from 0th monkeys (array pointer to zero) steps are the same as above
* Until the counter reaches the $ m value * The final $ I value obtained is the next monkey to be kicked out of the team.
Subid
*/
// Set the counter
For ($ j = 1; $ j <= $ m; $ j ++)
{
// Monkey Id 1, review a monkey
$ I ++;
// If the monkey is not kicked out of the team, obtain the next monkey number
If ($ monkey [$ I]> 0) continue;
// If the element value is 0, the monkey has been kicked out of the team, and then cyclically removes a monkey.
No.
If ($ monkey [$ I] = 0)
{
// Remove a monkey ID
For ($ k = $ I; $ k <$ len; $ k ++)
{
// The value is 0, and the number is 1
If ($ monkey [$ k] = 0) $ I ++;
// Otherwise, the serial number is obtained and exit
If ($ monkey [$ k]> 0) break;
}
}
// If the number is greater than the number of monkeys, traverse from 0th monkeys (array pointer to zero)
Same Steps
If ($ I = $ len) $ I = 0;
// Obtain the ID of the next monkey
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;
// The number interval of the team to be kicked out
$ M = 3;
// Call monkey to obtain the Function
GetKingMokey ($ n, $ m );
?>
Recursive Algorithms
$ Monkeys = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // monkey ID
$ M = 4; // The number of monkeys to be kicked out
/* 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 Monkey King ";
Return;
}
Else {
While ($ num ++ <$ m ){
$ Current ++;
$ Current = $ current % $ number;
}
Echo $ monkeys [$ current]. "The monkey was kicked out.
";
Array_splice ($ monkeys, $ current, 1 );
KillMonkey ($ monkeys, $ m, $ current );
}
}
KillMonkey ($ monkeys, $ m );