代码如下:
def hano(n,x,y,z): if n==1: print(x,"->",z) else: #将n-1个盘子从x->y hano(n-1,x,z,y) #将剩余的最后一个盘子从x->z print(x,"->",z) #将剩余的n-1个盘子从y->z hano(n-1,y,x,z) n = int(input("请输入汉诺塔的层数:")) hano(n,"A","B","C")
代码如下:
def hano(n,x,y,z): if n==1: print(x,"->",z) else: #将n-1个盘子从x->y hano(n-1,x,z,y) #将剩余的最后一个盘子从x->z print(x,"->",z) #将剩余的n-1个盘子从y->z hano(n-1,y,x,z) n = int(input("请输入汉诺塔的层数:")) hano(n,"A","B","C")