Php implements Monkey King Election Algorithm instance, Monkey King

Source: Internet
Author: User

Php implements Monkey King Election Algorithm instance, Monkey King

This example describes how to implement the Monkey King Election Algorithm in php. Share it with you for your reference. The specific analysis is as follows:

I. Problems:

N monkeys sat in a circle and numbered from 1 to n clockwise.
Then, the number of monkeys started to report data from 1 clockwise and reported to m's monkeys for exit. Then, the number of monkeys started to report data again from the next position,
Repeat this until there is one monkey left. It is the king.

Design and write programs to implement the following functions:
(1) The number of monkeys starting from the user input is required $ n, and the last number of records $ m.
(2) give the initial number of the Monkey King.

Ii. solution:

/*** @ Param int $ n indicates the number of monkeys at the beginning * @ param int $ m indicates the last number of monkeys reported * (the monkeys reported for this number are eliminated, then the next monkey returns the count from ①) * @ return int the initial number of the monkey */function monkeySelectKing ($ n, $ m) {// the initial number of monkeys cannot be less than 2 if ($ n <2) {return false;} $ arr = range (1, $ n ); // divide the monkey into an array. The array value corresponds to the monkey's initial number $ unsetNum = 0; // defines a variable and records the monkey's report number for ($ I = 2; $ I <= $ n * $ m; $ I ++) // you do not know how to calculate the total number of cycles. {// However, because return is set in the loop, so $ m * $ len efficiency can also be foreach ($ arr as $ k => $ v) {$ unsetNum ++; // every monkey, monkey report + 1 // when the monkey report number is equal to the number of the eliminated monkey: the monkey is eliminated (the array element is deleted) // The report number is 0 (the next monkey starts from 1) if ($ unsetNum ==$ m) {// echo "<pre>"; // open the comment and you can see the specific elimination process // print_r ($ arr ); unset ($ arr [$ k]); // monkey elimination $ unsetNum = 0; // returns NULL if (count ($ arr) = 1) // judge the length of the array. If there is only one monkey left, return its value {return reset ($ arr) ;}}} var_dump (monkeySelectKing (6, 3 ));

Supplement the improved algorithm (this algorithm is more concise and clear !) :

function yuesefu($n,$m) {   $r=0;   for($i=2; $i<=$n; $i++) {      $r=($r+$m)%$i;   }  return $r+1; } print_r(yuesefu(3,3));

I hope this article will help you design php programming algorithms.

Related Article

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.