Python ALIVE AND DEAD – JosephLoendorf
使用队列(先进先出原理),代码如下:
from queue importQueue
N =30
q =Queue(N) #建立一个长度为30的堆栈
for i in range(1, N +1): #给堆栈赋值1~30
q.put(i)
#print(q.get())
check =0 #报数检测9
leaveN =0 #下船人数记录
while leaveN <15:
check +=1
temp = q.get() #前进先出
if check ==9:
print(f'{temp}号下船')
check =0
leaveN +=1
else:
q.put(temp) #进入队列