汉诺塔(hanoi)递归实验
- 函数的含义,有n个盘子,从a 经过b 移动到c
- 中间的步骤如下图,第一步直接调用自身做递归,第二步只有一部所以直接打印出来,第三步直接调用自身做递归

def hanoi(n, a, b ,c):
if n > 0:
hanoi(n-1,a, c, b)
print('moving from %s to %s'%(a,c))
hanoi(n-1,b, a, c)
hanoi(3,'A', 'B', 'C')
'''
moving from A to C
moving from A to B
moving from C to B
moving from A to C
moving from B to A
moving from B to C
moving from A to C
'''
本文介绍了汉诺塔问题的递归解决方案,通过一个具体的实例展示了如何使用Python实现这一过程。代码中详细解释了函数调用的步骤,帮助理解递归思想在解决复杂问题中的应用。
785

被折叠的 条评论
为什么被折叠?



