#hanoi.py
def hanoi(n,x,y,z):
if n==1:
print(x,"-->",z)
else:
hanoi(n-1,x,z,y)
print(x,"-->",z)
hanoi(n-1,y,x,z)
hanoi(2,"X","Y","Z")
print("----------")
hanoi(3,"X","Y","Z")

结果如下:

python:递归函数(汉诺塔)-LMLPHP

05-02 15:37