Php Joseph's problem solving algorithm for killing prisoners. Php Joseph's problem solving the algorithm for killing prisoners this article mainly introduces php Joseph's problem solving algorithm for killing prisoners, the example analyzes php's implementation of the Joseph problem and php's algorithm for solving the Joseph problem
This article mainly introduces the php Joseph's problem solving algorithm for killing prisoners. The example analyzes php's implementation and application skills on Joseph's problem, which has some reference value. if you need it, you can refer to it.
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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Function getNum ($ n, $ m ){ // Store all the numbers in the array for initialization $ A = array (); // Traverse and store the data 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 the elements in the loop. // Remove rules For ($ j = 1; $ j <= $ m; $ j ++ ){ // If the last entry of the array is not reached If (next ($ )){ If ($ j = $ m ){ // Delete m items Unset ($ a [array_search (prev ($ a), $ a)]); } } Else { // If next does not exist, the pointer is 0. Reset ($ ); If ($ j = $ m ){ Unset ($ a [array_search (end ($ a), $ a)]); Reset ($ ); } } } } Return current ($ ); } Echo getNum (5, 3 ); |
I hope this article will help you with php programming.
This article introduces the php Joseph's problem solving algorithm for killing prisoners. The example analyzes php's implementation of the Joseph's problem and...