python汉诺塔问题

 

 1 count=0
 2 def hanoi(n,src,dst,mid):
 3     global count
 4     if n==1:
 5         print("{}:{}->{}".format(1,src,dst))
 6         count+=1
 7     else:
 8         hanoi(n-1,src,mid,dst)
 9         print("{}:{}->{}".format(n,src,dst))
10         count+=1
11         hanoi(n-1,mid,dst,src)
12 n=eval(input())
13 hanoi(n,"A","C","B")
14 print(count)

 

posted @ 2020-03-29 22:15  木彳  阅读(247)  评论(0编辑  收藏  举报