欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python练习题11:汉诺塔实践

程序员文章站 2024-03-05 15:26:55
...

Python练习题11:汉诺塔实践

steps = 0
def hanoi(src,des,mid,n):
	global steps
	if n == 1:
		steps += 1
		print("[STEP{:>4}] {}->{}".format(steps,src,des))
	else:
		hanoi(src,mid,des,n-1)
		steps += 1
		hanoi(mid,des,src,n-1)
n = eval(input())
hanoi("A","C","B",n)		
相关标签: Python python