题目内容:
如在汉诺塔游戏中,我们希望将塔A上的n个盘子,通过塔B移动到塔C,则对于任意输入的n,给出移动的步骤。
输入格式:
一个正整数n
输出格式:
移动的步骤
输入样例:
2
输出样例:
Move 1 from A to B
Move 2 from A to C
Move 1 from B to C
def hanoti(n,a,b,c):
if n == 1:
print('Move',n,'from',a,'to',c)
else:
hanoti(n-1,a,c,b)
print('Move',n,'from',a,'to',c)
hanoti(n-1,b,a,c)
n=int(input())
hanoti(n,'A','B','C')