python_递归函数

汉诺塔算法:

>>> def hnt(n,a,b,c):
...     if n==1:
...             print(a,'-->',c)
...     else:
...             hnt(n-1,a,c,b)
...             hnt(1,a,b,c)
...             hnt(n-1,b,a,c)
...
>>> hnt(3,'A','B','C')
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C

你可能感兴趣的:(python_递归函数)