Josephus issue Java chaos
The daughter is sick for a few days, and I also have to feel the greatness of the medical industrialization while I am worried about my daughter. Under the guidance of the great spirit of money, the doctor's work is active. Unprecedented improvement, "Open Thoughts, Active Collection" Become a hot topic of various hospitals. I think everyone also discovered this situation, that is, "There is no disease as a disease, the small disease is more ill, and the big illness is much more seriously ill." In order to let everyone see the disease, the medical industrialization is not to let it leave, and the hospital is also a holy time.
Oh, I said to leave, I can't help but let me think of a algorithm problem in the program:
It is said that there are 10 children to make a game, from the third child, clockwise, time to the 5th child, the child leaves. The child is constantly leaving, the circle is constantly narrowing, and the last child is a victor. Everyone may say, isn't this Josephus issue?
Yes, this is the Josephus issue. Do you know about this problem?
The Josephus problem is based on a report of historian Joseph Ben Matthias (become Josephus). The report tells that he and 40 soldiers have signed by the Roman army by the Roman army, Josephus suggestions Everyone kills his neighbors, and he is very smart to make himself a last one of these people, so it has been survived. Oh, is this person who is bad?
Ok, let's take a look at this allusion, let's take a look at the specific algorithm of the Josephus issue.
Public class josephus {
Public static void main (string args [])
{
INT NUM = 10; // Total number of children
INTITRVAL = 5; // Every time INTERVAL a child, let the child leave
int [] child = new int [NUM 1]; // Child Tree Group
int [] flag = new int [NUM 1]; // Each child is in the circle, 1: at 0: Not
For (int i = 1; i <= NUM; i ) {
Child [i] = i;
Flag [i] = 1; // Start every child is in the circle
System.out.println ("" i "child's name:" Child [i]);
}
INT n = 0;
INT i = 3; // Starting from the first few children
INT j = 1; // Start from 1
Boolean noend = true; // End of the flag
While (noend)
{
While (j i = (i 1> NUM? 1: i 1); J = flag [I]; } FLAG [I] = 0; N ; IF (n == Num) { NOEND = false; System.out.println ("No. i " Some Children's Last Victory "); } Else { System.out.println ("No. i " a child leaves "); J = 0; // jG reaches Interval, from the new beginning } } } } Everyone can change the number to try it, very interesting. View the execution result, I always feel that there is something rule here, but I have not summed it yet. If the friend can sum up, I hope you know. Finally, let's give you the following stuff to everyone, even make a concluding gift. The hospital is not a cinema, I can see it. The hospital is not a welfare home, I don't want to take it. The hospital is not a nursing home, not to raise it. The hospital is not a State Council, not going to enter.