一、队列操作
题目
思路
利用collections.deque()双向队列实现
代码
import collections
a = collections.deque()
n = int(input())
for i in range(n):
temp = list(map(int,input().split()))
if len(temp) == 1:
if temp[0] == 2:
if a:
print(a.popleft())
else:
print("no")
else:
print(len(a))
else:
x,y = temp
a.append(y)
二、汉诺塔
题目
思路
递归
代码
n,m = list(map(int,input().split()))
cnt = 0
def hanoi(x,y,z,n):
global cnt
if n == 1:
cnt += 1
## print(f"#{n}: {x}->{z}")
if cnt == m:
print(f"#{1}: {x}->{z}")
else:
hanoi(x,z,y,n-1)
cnt += 1
## print(f"#{n}: {x}->{z}")
if cnt == m:
print(f"#{n}: {x}->{z}")
hanoi(y,x,z,n-1)
hanoi("A","B","C",n)
print(cnt)