PHP implementation of Joseph Ring

Source: Internet
Author: User

PHP implementation of Joseph Ring

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.

Directory
• Method 1 is removed logically
• Method 2 Recursive Algorithms
• Method 3: Linear table Application
Method 1 Delete and edit the directory in sequence by logic
<? Php <br/>/* <br/> * filename: kingmonkey. php <br/> * Author: Luochuan Wang <br/> * Date: rjl 2nd, 2009 <br/> * descript: an arithmetic to a program <br/> * program: <br/> A group of monkeys are arranged in a circle. Press 1, 2 ,..., N is numbered in sequence. <Br/> then, The number starts from 1st and then starts to M. Then, the number starts from and then to M. <br/>, kicking it out ..., <br/> the monkey is called the King until there is only one monkey. <Br/> it is required to program and simulate this process. Input M and N, and output the number of the last king. <Br/> */</P> <p> function getkingmokey ($ N, $ m) <br/>{< br/> $ monkey [0] = 0; </P> <p> // sequentially number 1-N monkeys into the array <br/> for ($ I = 1; $ I <= $ N; $ I ++) <br/>{< br/> $ monkey [$ I] = $ I; <br/>}</P> <p> $ Len = count ($ monkey); </P> <p> // cyclically traverse array elements (monkey ID) <br/> for ($ I = 0; $ I <$ Len; $ I = $ I) <br/>{< br/> $ num = 0; </P> <p>/* <br/> * traverses the $ monkey array and calculates the number of elements whose values are not 0 (the number of remaining monkeys) <br/> * value: $ num, obtain the element value of an element whose value is not 0 <br/> */<br/> foreach ($ monkey as $ key => $ Value) <br/>{< br/> if ($ value = 0) continue; <br/> $ num ++; <br/> $ values = $ value; <br/>}</P> <p> // if only one monkey is left, the Monkey number (array element value) is output) and exit the loop <br/> if ($ num = 1) <br/>{< br/> echo $ values; <br/> exit; <br/>}</P> <p>/* <br/> * if the number of remaining monkeys is greater than 1 ($ num> 1) <br/> * continue Program <br/> */</P> <p> // force the $ I monkey out of the team (set the element value of the corresponding array position to 0) <br/> $ monkey [$ I] = 0; </P> <p> // print the monkey position <br/> echo $ I. ""; </P> <p>/* <br/> * obtain the number of the monkey that needs to be kicked out of the team. <br/> * traverse the monkey within the range of $ m and set $ M counter <Br/> * sequentially remove the Monkey number <br/> * If the element value is 0, the monkey in the position has been kicked out of the team <br/> * if not 0, continue to get the next monkey number, and the counter is incremented by 1 <br/> * if the number of monkeys obtained is greater than the number of arrays <br/> *, The traversal starts from 0th monkeys (array pointer to zero) steps are the same as above <br/> * until the counter reaches the $ m value * The final $ I value obtained is the monkey Number of the next team to be kicked out <br/> */</P> <p> // set the counter <br/> for ($ j = 1; $ j <= $ m; $ J ++) <br/>{< br/> // monkey ID plus one, repeat a monkey <br/> $ I ++; </P> <p> // If the monkey is not kicked out of the team, obtain the next monkey number <br/> if ($ monkey [$ I]> 0) continue; </P> <p> // If the element value is 0, the monkey has been kicked out of the team and then cyclically removes a monkey number <br/> if ($ monkey [$ I] = 0) <B R/>{< br/> // remove a monkey number <br/> for ($ k = $ I; $ k <$ Len; $ K ++) <br/>{< br/> // The value is 0 and the number is plus 1 <br/> if ($ monkey [$ K] = 0) $ I ++; </P> <p> // otherwise, the number is obtained and the result is exited. <br/> if ($ monkey [$ K]> 0) break; <br/>}</P> <p> // if the number is greater than the number of monkeys, traverse from 0th monkeys (array pointer to zero) the preceding steps are as follows: <br/> if ($ I = $ Len) $ I = 0; </P> <p> // same as the preceding steps, get the next monkey number <br/> if ($ monkey [$ I] = 0) <br/>{< br/> for ($ k = $ I; $ k <$ Len; $ K ++) <br/>{< br/> if ($ monkey [$ K] = 0) $ I ++; </P> <p> if ($ monkey [$ K]> 0) break; <br/>}</P> <p> // Number of monkeys <br/> $ n = 10; </P> <p> // The number interval between the kicked-out teams <br/> $ M = 3; </P> <p> // call the Monkey King function </P> <p> getkingmokey ($ N, $ m); </P> <p >?> <Br/>
Method 2 Recursive Algorithms

$ Monkeys = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // monkey id <br/> $ M = 4; // The monkey on the count is kicked out. <br/>/* Monkey King algorithm */<br/>/* process-oriented implementation */<br/> Function killmonkey ($ monkeys, $ M, $ current = 0) {<br/> $ number = count ($ monkeys); <br/> $ num = 1; <br/> If (count ($ monkeys) = 1) {<br/> echo $ monkeys [0]. "Become Monkey King"; <br/> return; <br/>}< br/> else {<br/> while ($ num ++ <$ m) {<br/> $ current ++; <br/> $ current = $ current % $ number; <br/>}< br/> echo $ monkeys [$ current]. "The monkey is kicked off <br/>"; <br/> array_splice ($ monkeys, $ current, 1); <br/> killmonkey ($ monkeys, $ m, $ current); <br/>}< br/> killmonkey ($ monkeys, $ m );

Finally, some netizens explained the best algorithm:

Juganainiu
Oh, well, after each monkey is listed, the rest of the monkeys constitute another sub-problem. But their numbers have changed. The first monkey to be listed must be a [1] = m (MOD) n (remainder of M/N). The other monkey to be removed is a [1] + 1, A [1] + 2 ,..., N, 1, 2 ,... A [1]-2, a [1]-1, corresponding to the new numbers 1, 2, 3... N-1. Assume that the new ID of a monkey is I, and the original ID is (I + A [1]) % N. Therefore, this forms a recursive problem. If we know that the solution of this Sub-problem (n-1 monkeys) is X, then the solution of the original problem (N monkeys) is: (x + M % N) % N = (x + M) % N. Start condition of the problem: If n = 1, the result is 1.

Method 3: Apply a linear table to edit the returned directory

<? <Br/>/** <br/> * linear table application (leader selection problem) <br/> * @ Param $ n total <br/> * @ Param $ m when the number of messages reaches m, M columns <br/> * @ return the remaining number <br/> */<br/> function yuesefu ($ N, $ m) {<br/> $ r = 0; <br/> for ($ I = 2; $ I <= $ N; $ I ++) {<br/> $ r = ($ R + $ m) % $ I; <br/>}< br/> return $ R + 1; <br/>}< br/> print_r (yuesefu (3, 3); <br/>?>

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.