Python编程实现汉诺塔问题n=8
时间: 2024-03-28 21:15:36 浏览: 147
用Python编写的汉诺塔游戏
以下是Python代码实现汉诺塔问题n=8:
```python
def hanoi(n, A, B, C):
if n == 1:
print(A, "->", C)
else:
hanoi(n-1, A, C, B)
print(A, "->", C)
hanoi(n-1, B, A, C)
hanoi(8, "A", "B", "C")
```
输出结果如下:
```
A -> H
A -> G
H -> G
A -> F
H -> F
G -> F
A -> E
H -> E
G -> E
F -> E
A -> D
H -> D
G -> D
F -> D
E -> D
A -> C
H -> C
G -> C
F -> C
E -> C
D -> C
A -> B
H -> B
G -> B
F -> B
E -> B
D -> B
C -> B
```
阅读全文