PHP Monkey Choice King Popular understanding

Source: Internet
Author: User
Personal summary of the monkey selected the King's Code, attached a note, for beginners will be very helpful, because I learned this time, understand Leng spent two or three hours, only to understand! Of course the code is ever changing! Before also saw a more concise, 5-6 lines on the written out, this varies from person to man, beginners still have to step by step!
2015 8.24 0:46

1 functiongetking($m,$n){2$arr=Array();//define an array for storing monkeys;3$a="a";//define the monkey number;4 for($i=0;$i<$m;$i++){//Start looping to each monkey by attaching a number;5$arr[] =$a++;//Loops A to M (gives the number of monkeys);6}78$i=0;//define a variable to be counted;9 while(Count ($arr) >1){//Get the number of monkeys, as long as the monkeys not less than 1 have been circulatingTenif($i%$n==0){//reported to the number of N to take the remainder (the number of sets, reported a few on a few to take the remainder of 0, then the monkey was proposed game) Oneunset($arr[$i]);//Delete an array element with a remainder of zero A}Else{ -$arr[] =$arr[$i];//If the array element is deleted if it is 0 , it is added to the end of the queue, and the next round continues to count, -unset($arr[$i]);//Delete the array element that you just added, which is abstract: the number of required numbers such as 5, 1 2 3, 4th-bit monkeys are not reported, then add -}//1 2 3 4 for the end of the squad, the lower round continues, while the 5th-bit monkey reported 5, was presented the game, the 6th-bit monkeys from 1, repeated so, -$i++; -} +return$arr; -} +Print_r (Getking ( -,5));

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above introduces the PHP monkey choice King Popular understanding, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.