# 热土豆 队列应用 class Queue(): def __init__(self): self.items = [] def isEmpty(self): return self.items ==[] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def __str__(self): return str(self.items) def size(self): return len(self.items) def hotPotato(namelist, num): simqueue = Queue() for name in namelist: simqueue.enqueue(name) while simqueue.size() > 1 : for i in range(num): simqueue.enqueue(simqueue.dequeue()) print(simqueue) simqueue.dequeue() return simqueue.dequeue() print(hotPotato([1,2,3,4,5,6], 7 ))
构造循环队列,每7次循环完,就出队一个杀一个人,全部循环完之后剩下了最后一个人不用死