topic:
For ease of discussion, the first problem will be described in accordance with the original intent of the language of mathematics.
Problems: The number of 0 ~ (N-1) arranged in a circle which N individuals, the number of packets in the clockwise start from 0, M-1 human report circular queue exit, the remainder of the number of packets continues from zero ,repeatedly. Finally, find the first number in the column by a circular queue.
Listed first below 0 ~ (N-1) N This original individual numbered as follows:
The foregoing process was found derived, the first column of a person's number must be (M-1)% n. For example, in 41 individuals, 3 people report if the column is a first column of a person must be a number (3-1) 2 = 41%, number noted here is zero, so the actual number 2 1 corresponds to the number 3 as a starting point. The foregoing description, a front element of m (M-1) has a column, a list of the people that are listed as follows:
According to the rules, when it was a column, and from the next to the position number of packet 0 is started, the above list is adjusted (i.e., the start position in the form of M, then N-1 followed by the 0,1,2 ...... form a cyclic):
Arranged in order are renumbered above, the following correspondence relationship obtained:
That is, the one on the data row would be to reorganize the list 0 ~ (N-2) N-1 individuals were, seeking to continue the n-1 participants, the number of packets according to an M-1 column i.e., Solution the last number in the first column by a circular queue.
What the law does not see? Yes, at one time treatment, the scale of the problem is reduced. That is, the number N of individuals reported problems may be solved first decomposed into (N-1) number of sub-problems of the individual packets; whereas for the sub-questions (N-1) number of individual packets, a first order to be decomposed [(N -1) sub-problems. -1] number of people reported .......
The minimum size of the problem is what? That is, when only one person (N = 1), reported that the number to (M-1) out of the line of people, who then finally the column is? Of course, only the number is 0 this person. Accordingly, the following functions may be provided:
Then, when N = 2, the number of packets to (M-1) column of a person, who last column of the? Should get the number reported only one person out of the last column of numbers plus M, M-1 report because people have been out of the line, only 2 people, the other is the last person out of the line of columns, can be formulated as the following form:
By calculating the above formula, the result F (2) may exceed the value of N (total number). For example, set N = 2, M = 3 (i.e. 2 individuals, the number of packets to the column 2), the calculated value of the press is obtained:
A total of only two people involved, No. 3 who apparently did not. How to do? Because it is reported that the number of cyclic, so after two complete packet number, numbered from 0 and from the number of packets then began. According to this principle, the total number of values can be obtained N modulo operation, namely:
5.5.4 mathematically Josephus Solution (2)
I.e., N = 2, M = 3 (i.e., there are two individuals, who reported that the number of the column to 3-1), the cycle number of the last dequeued packets numbered 1 person (numbering begins with 0). We look to project, as shown below, when the number of cycles of the two packets numbered 0, the number of people reported a number of 0 to 2 and 0, when the register 2 (M-1), the column number 0 , the last remaining person number 1, so the number of people out of 1 last column.
The above derivation can be easily deduced, when N = 3 at formula:
Similarly, we can also deduce when the number of participants is N, the last column personnel numbered formula:
In fact, this is a recurrence formula, the formula contains the following two formulas:
(Many people here do not understand why + M, for example, we consider only two people numbered 0,1, M is equal to 3. Because the last one out of the queue number must be 0. So the penultimate out queue numbers must be 1, M plus the nature of that the M-1 first bound to the column, so that the M-th to the last column, then consider the relationship circular queue, so take the remainder!)
With this recursion formulas, again very simple design program can be used to design a program recursive methods, specific code as follows:
- #include <stdio.h>
- int main(void)
- {
- int n,m,i,s=0;
- the printf ( "Enter the number of columns involved in the position M and N values = ');
- scanf('%d%d',&n,&m);
- printf ( 'the last person out of the line of initial location is% d \ n', josephus (n, m));
- getch();
- return 0 ;
- }
- int josephus(int n,int m)
- {
- if(n==1)
- return 0;
- else
- return (josephus(n-1,m) m)%n;
- }
In the above code, Josephus defines a recursive function (), and then calls the function in the main function operation.
Compiler performing the above procedure, the input values of N and M, can quickly obtain the final column number of persons, the input N = 8, M = 3, Figure 5-19 shows the results obtained (note numbering begins with 0) .
Recursive function will take up more computer memory, when a recursive hierarchy is too deep may cause the program can not be executed, therefore, the program may be written directly into the following recursive form:
- #include <stdio.h>
- int main(void)
- {
- int n,m,i,s=0;
- the printf ( "Enter the number of columns involved in the position M and N values = ');
- scanf('%d%d',&n,&m);
- for (i=2; i<=n; i )
- s=(s m)%i;
- printf ( 'the last person out of the line of initial location is% d \ n', s);
- getch();
- return 0 ;
- }
The results of this recursive procedure code execution and the execution results are identical.
As can be seen, after some mathematical derivation and, finally, a simplified procedure law, dozens of lines of code reduced to a few lines. More importantly, the efficiency of program execution have greatly advanced, eliminating a lot of repetitive cycles, even solving large N and M values, it will not be a problem