# 汉诺塔问题:将在x柱上从小到大的n个圆盘按照从小到大的顺序移到z柱
# 递归思想:先将n-1个圆盘借助z,移到y上;再将最底下的一个个圆盘移到z上
# 最后将y上的n-1个圆盘移到z上
1 def hanoi(n, x, y, z): 2 global count 3 if n == 1: 4 print(x, '-->', z) 5 count += 1 6 else: 7 hanoi(n - 1, x, z, y) # 将n-1个圆盘借助z,移到y上 8 print(x, '-->', z) # 将最底下的一个个圆盘移到z上 9 count += 1 10 hanoi(n - 1, y, x, z) # 将y上的n-1个圆盘移到z上 11 12 13 count = 0 14 n = int(input('请输入汉诺塔的层数:')) 15 hanoi(n, 'x', 'y', 'z') 16 print('移动的总次数为:', count)