"Joseph Ring" is a mathematical application of the problem: a group of monkeys in a circle, according to 1,2, ..., n followed by numbers. Then from the first start, the number to the mth, kick it out, start it behind it, count it again, kick it out ... and keep doing so until the end Only one monkey left, the monkey is called the king. Requires programming simulation of this process, enter m, n, output the last king number.
Here are three ways to solve this problem with PHP:
Recursive algorithms are sequentially removed logical application methods a linear form: logically sequentially removed 1http: //www.aliyun.com/zixun/aggregation/19060.html "> 23456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384functiongetKingMokey ($ n, $ m) {$ monkey [0] = 0; // serialize 1-n monkeys into an array for ($ i = 1; $ i <= $ n; $ i ++) {$ monkey [$ i] = $ i;} $ len = count ($ // loop over the array elements (monkey numbers) for ($ i = 0; $ i <$ len; $ i = $ i) {$ num = 0; The number of elements not 0 (the number of remaining monkeys) * The value of $ num and the value of the element whose value is not 0 * / foreach ($ monkeyas $ key => $ value) {if ($ value = = 0) continue; $ num ++; $ values = $ value;} // If only one monkey is left, then output the monkey number (array element value) and exit the loop if ($ num == 1) {re turn $ programs; exit;} / * * If the number of remaining monkeys is greater than 1 ($ num> 1) * Proceed to the program * / // Kick the $ i monkey out of the team (the corresponding array position element value is set to 0) $ monkey Get the next monkey to be kicked out of the team * Walk the monkey over the value of $ m and set the counter for $ m * Remove the monkey number one at a time * If the element value is 0, Then the monkey in this position has been kicked out of the team. * If not, continue to get the next monkey number, and the counter plus 1 * If the obtained monkey number is greater than the number of the array * Zero) Steps ibid. * Until the counter reaches the value of $ m * The last value of $ i is the number of the next monkey to be kicked out of the team * / / Set the counter for ($ j = 1; $ j <= $ m; $ j ++) {// add a monkey number, traverse the next monkey $ i ++; // if the monkey has not been kicked out of the team, access to the next monkey number if ($ monkey [$ i]> 0) continue; // If the value of the element is 0, the monkey has been kicked out of the queue and a loop is taken to remove a monkey if ($ monkey [$ i] == 0) / Remove a monkey number for ($ k = $ i; $ k <$ len; $ k ++) {// value is 0, the number plus 1 if ($ monkey [$ k] == 0) $ i ++; / / Otherwise, the number has been obtained, exit if ($ monkey [$ k]> 0) break;}} / / If the number is greater than the number of monkeys, begin traversing the first 0 monkeys (array pointer return to zero) $ i == $ len) $ i = 0; // same step, 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 of the team numbered interval value $ m = 3; / / call Monkey King get function echogetKingMokey ($ n, $ m). "Monkey King"; Method two: recursive algorithm 12345
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.