This article mainly introduces the solution to the php monkey dawang problem, which is essentially the Joseph Ring problem. Here we provide a simple solution. If you need it, refer
This article mainly introduces the solution to the php monkey dawang problem, which is essentially the Joseph Ring problem. Here we provide a simple solution. If you need it, refer
This article describes how to solve the php monkey dawang problem. Share it with you for your reference. The specific analysis is as follows:
Problem description:
A group of monkeys are arranged in a circle and 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, count to m, and kick it out ..., the monkey is called the King until there is only one monkey. Require programming to simulate this process, input m, n,
Output the number of the last king.
Solution:
<? Phpfunction king ($ m, $ n) {for ($ I = 1; $ I <$ m + 1; $ I ++) {// construct an array $ arr [] = $ I ;}$ I = 0; // set the array pointer while (count ($ arr)> 1) {// traverse the array, determine whether the current monkey is the number of out, // if yes, It is out; otherwise, put it to the end of the array if ($ I + 1) % $ n = 0) {unset ($ arr [$ I]);} else {array_push ($ arr, $ arr [$ I]); // unset ($ arr [$ I]) at the end of the array for this round of non-outbound monkeys; // Delete} $ I ++;} return $ arr ;} var_dump (king ();?>
I hope this article will help you with php programming.