Easy Tutorial
❮ Python Conditional Statements Ref Math Erf ❯

Python Josephus Survivor and Casualty Game

Python3 Example

30 people are on a boat, which is overloaded, requiring 15 people to get off.

So, people line up in a queue, and their positions in the queue are their numbers.

They count off, starting from 1, and every 9th person steps off the boat.

This continues until only 15 people remain on the boat. The question is, which numbered individuals get off the boat?

Example

people={}
for x in range(1,31):
    people[x]=1
# print(people)
check=0
i=1
j=0
while i<=31:
    if i == 31:
        i=1
    elif j == 15:
        break
    else:
        if people[i] == 0:
            i+=1
            continue
        else:
            check+=1
            if check == 9:
                people[i]=0
                check = 0
                print("{}号下船了".format(i))
                j+=1
            else:
                i+=1
                continue

Executing the above example, the output is:

9号下船了
18号下船了
27号下船了
6号下船了
16号下船了
26号下船了
7号下船了
19号下船了
30号下船了
12号下船了
24号下船了
8号下船了
22号下船了
5号下船了
23号下船了

Python3 Example

❮ Python Conditional Statements Ref Math Erf ❯