Discuss / Python / 交作业

交作业

Topic source

Afternoon Tea

#1 Created at ... [Delete] [Delete and Lock User]

def move(n, a, b, c):
n = int(n)
if n <= 0:
raise TypeError('bad operand n')
if n == 1:
print(a, '-->', c) # 如果a中的盘子只有一个那么直接移动到c盘上
elif n > 1:
move(n - 1, a, c, b) # a中的最大的底盘移动到c时,需要将n-1个盘子借助c移动到b上,然后再将最后的最大底盘移动到c
print(a, '-->', c) # 把最底下的盘子从 a 移动到 c 上
move(n - 1, b, a, c) # b上面有n-1个盘子借助a移动到c的问题


  • 1

Reply