Python写的汉诺塔

简单模式:
def hanoi(a, b, c, n):
	if n <= 0:
		print("All moves finished")
	if n == 1:
		print("move", n, "from",a,"to", c)
	else:
		hanoi(a, c, b, n - 1)
		print("move", n, "from",a,"to", c)
		hanoi(b, a, c, n - 1)

hanoi("a","b","c", 5);


四柱汉诺塔:
def fourHanoi(a, b, c, d, n):
	if n == 1:
		print("move", n, "from", a, "to", d)
	elif n == 2:
		print("move", n - 1, "from", a, "to", b)
		print("move", n, "from", a, "to", d)
		print("move", n - 1, "from", b, "to", d)
	else:
		fourHanoi(a, b, d, c, n - 2)
		print("move", n - 1, "from", a, "to", b)
		print("move", n, "from", a, "to", d)
		print("move", n - 1, "from", b, "to", d)
		fourHanoi(c, a, b, d, n - 2)
fourHanoi("a", "b", "c", "d", 4)

你可能感兴趣的:(C++,c,python,C#)