python汉诺塔五层
时间: 2023-11-17 12:02:56 浏览: 69
五阶汉诺塔
下面是Python实现汉诺塔五层的代码:
```python
def hanoi(n, a, b, c):
if n > 0:
hanoi(n-1, a, c, b)
print("move %d from %s to %s" % (n, a, c))
hanoi(n-1, b, a, c)
hanoi(5, 'A', 'B', 'C')
```
上述代码中,`hanoi`函数接受四个参数,分别是盘子的数量`n`,起始柱子`a`,中间柱子`b`和目标柱子`c`。在函数内部,首先判断盘子数量是否大于0,如果是,则递归调用`hanoi`函数,将`n-1`个盘子从起始柱子移动到中间柱子,然后将最后一个盘子从起始柱子移动到目标柱子,最后再将`n-1`个盘子从中间柱子移动到目标柱子。
运行上述代码,可以得到如下输出:
```
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 4 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 5 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 5 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from C to A
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 5 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 4 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 5 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 4 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 5 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 4 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 4 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 4 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 5 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 4 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 4 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 5 from C to A
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 4 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 3 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 4 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from C to B
move 1 from A to C
move 2 from A to B
move 1 from C to B
move 3 from A to C
move 1 from B to A
move 2 from B to C
move 1 from A to C
move 3 from B to A
move 1 from C to B
move 2 from C to A
move 1 from B to A
move 4 from B to C
move 1 from C to B
move
阅读全文