Joseph life and death game (Simple Solution) - python

Let me talk about the game of life and death issues Joseph description:

30 Ge passengers ride the same boat, because of severe overloading, coupled with high winds and big waves, extremely dangerous; therefore the captain to tell you, only half of the whole ship passengers into the sea, to the rest survived. Helpless, we had to agree with this approach and agreed to 30 personal circle, starting from the first person, in turn reported the number to the number of 9 individuals, they took him into the sea, and then start the next person from his from a number of cases, the number of 9 individuals, and then she put into the sea, and so on, until the remaining 15 individual passengers so far. Q. What position is the position to be thrown into the sea.

  

  Many people think this problem is to use a linked list to resolve, but the list is cumbersome and inflexible causing a lot of c faithful to resist the emotions

That there is no simple and fast solution it

Of course yes

The following wording can use any programming language, I'll show you first with python

First on the code, and then resolve what my thinking

{} = A 
for I in Range (1,31):
A [I]. 1 =
Sheng = 0
// here represented by the number of Sheng remaining
sum = 0
// is used to count the sum, when the sum is equal to nine when reset
I =. 1
the while I <= 31 is:
IF I == 31 is:
I =. 1
elif Sheng == 15:
BREAK
the else:
IF A [I] == 0:
I = I +. 1
Continue
the else:
SUM = + . 1
IF == SUM. 9:
a [I] = 0
SUM = 0
Print (I)
Sheng + =. 1
the else:
I = I +. 1
Continue
right, is so simple (if uncomment less code words)
said some ideas it:
(1) open space Placer} {A
(2) using the cycle is assigned a 1 to 30 (note that the position or index 0 0)
(3) while loop, the inner loop, i == 31 when jumping back, the number of the remaining time to end the cycle of 15, sum to nine when cleared, and the person's value becomes zero
so simple, do not need so complicated (although it can do it)
this idea can be used in any programming languages (Java, c, c ++, etc.)

Guess you like

Origin www.cnblogs.com/lihangluoyue/p/12372193.html