PHP Monkey Choice King Problem solving method, Monkey King _php Tutorial

Source: Internet
Author: User

PHP Monkey Choice King Problem solving method, Monkey King


This article is an example of how to solve the problem of PHP monkeys choosing King. Share to everyone for your reference. The specific analysis is as follows:

Problem Description:

A group of monkeys in a circle, according to 1,2,...,n sequentially numbered. Then, starting from the 1th, Count to M., Kick It Out of the loop, start counting from behind it, Count to M., and Kick It out ... and so on, until the last monkey is left, the monkey is called the King. Requires programming to simulate this process, enter M, N,
Output the number of the last king.

Workaround:

<?phpfunction King ($m, $n) {for ($i = 1; $i < $m + 1; $i + +) {//build array  $arr [] = $i;} $i = 0;//Sets the array pointer while (COUNT ($ ARR) > 1) {  //iterate through the array to determine whether the current monkey is out of sequence number,  //If it is out, otherwise put in the array last  if (($i + 1)% $n = = 0) {   unset ($arr [$i]);  } el SE {   array_push ($arr, $arr [$i]);   This round of non-out monkeys put array tail   unset ($arr [$i]);   Delete  }  $i + +;} return $arr;} Var_dump (King (100,5));? >

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/998550.html www.bkjia.com true http://www.bkjia.com/PHPjc/998550.html techarticle php Monkey Choice King Problem solving method, Monkey King This article describes the PHP monkey Choice King Problem solving method. Share to everyone for your reference. The specific analysis is as follows: Problem Description: ...

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