Python Josephus Living and Dead Mini Game

Document 对象参考手册Python3 Examples

30 people are on a boat, it is overloaded, and 15 people need to get off.

So they line up in a queue, and their position in the queue is their number.

Count off starting from 1; the person who counts to 9 gets off the boat.

Repeat this cycle until only 15 people remain on the boat. Which numbered people got off?

Examples

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("No.{} got off the boat".format(i)) j+=1 else: i+=1 continue

Execute the above example, and the output result is:

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

Document 对象参考手册Python3 Examples

Other Extensions