The monkey chooses the King, the Monkey King. The monkey chooses the king, and a group of monkeys in a circle, numbered 1, 2, and n. Then, start counting from 1st, count to m, kick it out of the circle, start counting from behind it, and count to Monkey King, Monkey King.
A group of monkeys are arranged in a circle ,..., N is numbered in sequence. Then start counting from 1st, count to m, kick it out of the circle, start counting from behind 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
// The basic idea is to determine whether a monkey is out of the game. if it is deleted, it is not placed at the end of the data.
Function xdw ($ m, $ n ){
For ($ I = 1; $ I <= $ n; $ I ++ ){
$ Arr [] = $ I;
}
For ($ I = 0; count ($ arr)> 1; $ I ++ ){
If ($ I + 1) % $ m = 0 ){
Unset ($ arr [$ I]);
} Else {
Array_push ($ arr, $ arr [$ I]);
Unset ($ arr [$ I]);
}
}
Return $ arr;
}
Print_r (xdw (2, 10 ));
A group of monkeys are arranged in a circle, numbered by 1, 2, and n. Then start counting from 1st, count to m, kick it out of the circle, start counting from behind it, and then count...