Php Joseph's problem solving-algorithms for killing prisoners-PHP Tutorial

Source: Internet
Author: User
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...

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.