python用递归函数解汉诺塔游戏
#汉诺达游戏 def hanni(n,x,y,z): if n == 1: print(x,'-->',z) else: hanni(n-1,x,z,y)#n-1移动到y print(x,'-->',z) hanni(n-1,y,x,z)#n-1移动到z n = int(input("请输入汉诺塔的层数:")) hanni(n,'X','Y','Z')
#汉诺达游戏 def hanni(n,x,y,z): if n == 1: print(x,'-->',z) else: hanni(n-1,x,z,y)#n-1移动到y print(x,'-->',z) hanni(n-1,y,x,z)#n-1移动到z n = int(input("请输入汉诺塔的层数:")) hanni(n,'X','Y','Z')
人生在与折腾、在于学习、在于进步