Monkey King, Monkey King _ PHP Tutorial

Source: Internet
Author: User
The monkey chooses the King, the Monkey King. The monkey chooses the king, and a group of monkeys in a circle, numbered 1, 2, and n. Then, start counting from 1st, count to m, kick it out of the circle, start counting from behind it, and count to Monkey King, Monkey King.

A group of monkeys are arranged in a circle ,..., N is numbered in sequence. Then start counting from 1st, count to m, kick it out of the circle, start counting from behind 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

// The basic idea is to determine whether a monkey is out of the game. if it is deleted, it is not placed at the end of the data.
Function xdw ($ m, $ n ){
For ($ I = 1; $ I <= $ n; $ I ++ ){
$ Arr [] = $ I;
}

For ($ I = 0; count ($ arr)> 1; $ I ++ ){
If ($ I + 1) % $ m = 0 ){
Unset ($ arr [$ I]);
} Else {
Array_push ($ arr, $ arr [$ I]);
Unset ($ arr [$ I]);
}
}
Return $ arr;
}
Print_r (xdw (2, 10 ));

A group of monkeys are arranged in a circle, numbered by 1, 2, and n. Then start counting from 1st, count to m, kick it out of the circle, start counting from behind it, and then count...

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.