# 汉诺塔问题,把n个盘子从A搬到C,在搬动过程中可以借助B作为中转。 cnt=0 #统计移动次数,cnt是一个全局变量 def hanoi(n,a,b,c): global cnt if n==1: cnt+=1 move(n,a,c) else: hanoi(n-1,a,c,b) cnt+=1 move(n,a,c) hanoi(n-1,b,a,c) def move(n,x,y): print("{:5d}: {:s}{:d}{:s}{:s}{:s}{:s}.".format(cnt,"Move disk ",n,"\ from tower ",x," to tower ",y)) def main(): print("TOWERS OF HANOI:") print("The problem starts with n plates on tower A.") print("Input the number of plates:") n=eval(input()) print("The step to moving {:d} plates:".format(n)) hanoi(n,'A','B','C') #借助B将n个盘子从A移至C main()