python学习-之汉诺塔实现
先上代码:
#move1/2方便分析循环时调用方法的逻辑顺序
def move1(n,a,b,c): print("-----------1号循环-----------") move(n,a,b,c) def move2(n,a,b,c): print("-----------2号循环-----------") move(n,a,b,c) def move(n,a,b,c): if n==1: return print("n=",n,"(","a=",a,"b=",b,"c=",c,")","***",a,"--->",c) else: move1(n-1,a,c,b) print("n=",n,"(","a=",a,"b=",b,"c=",c,")","***",a,"--->",c) move2(n-1,b,a,c) # print(n,"a1:",a,"b1:",b,"c1:",c) move(3,"A","B","C")
输出结果: