def hanoi(n, a, b, c):
if n == 1 :
print(a, '->', c)
else:
hanoi(n-1, a, c, b) # n-1个,从a到b
print(a, '->', c) # 第n个,从a到c
hanoi(n-1, b, a, c) # n-1个,从b到c
userInput = int(input('请输入层数:'))
hanoi(userInput, 'A', 'B', 'C')
网友评论