先上代碼:spa
#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")
輸出結果:3d