Php Joseph's problem solving: The algorithm for killing prisoners, php Joseph's prisoner algorithm _ PHP Tutorial

Source: Internet
Author: User
Php Joseph's problem solving algorithm for killing prisoners: php Joseph's prisoner algorithm. Php Joseph's problem solving algorithm for killing prisoners. This article describes examples of php Joseph's problem solving algorithm for killing prisoners. I would like to share with you the php Joseph's problem solving algorithm for prisoner killing, and the php Joseph's prisoner algorithm.

This article provides an example of php Joseph's algorithm for solving the problem of prisoner killing. Share it with you for your reference. The specific analysis is as follows:

In ancient times, a judge ordered the death of an IV prisoner. he had a absurd law that made the prisoner stand in a circle, starting from the number of individuals in s, and then pulled out and executed every time the number of persons in D was reached, then count D more and execute again ...... Until the last one can be forgiven.

Function getNum ($ n, $ m) {// used to save all the numbers to the array initialization $ a = array (); // traverses and stores them in the array for ($ I = 1; $ I <= $ n; $ I ++) {$ a [$ I] = $ I;} // the pointer is 0 reset ($ ); while (count ($ a)> 1) {// if the number of items in the array is greater than 1, continue to remove elements in the loop. // The removal rule for ($ j = 1; $ j <= $ m; $ j ++) {// if the last entry of the array is not reached, if (next ($ a) {if ($ j = $ m) {// delete m unset ($ a [array_search (prev ($ a), $ a)]) ;}} else {// If next does not exist, then the pointer is 0 reset ($ a); if ($ j = $ m) {unset ($ a [array_search (end ($ a), $ a)]); reset ($ a) ;}}} return current ($ a) ;}echo getNum (5, 3 );

I hope this article will help you with php programming.

Examples in this article describe the php Joseph's algorithm for solving the issue of prisoner killing. Share it with you for your reference...

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.