汉诺塔用python编写
以下是一个用Python编写的汉诺塔游戏的示例代码:
def hanoi(n, source, target, auxiliary):if n == 1:print(f"Move disk 1 from {source} to {target}")returnhanoi(n - 1, source, auxiliary, target)print(f"Move disk {n} from {source} to {target}")hanoi(n - 1, auxiliary, target, source)n = 3
hanoi(n, 'A', 'C', 'B')
你可以运行该代码,将其中的 n
指定为你想要玩的汉诺塔游戏的盘数,然后运行代码即可看到汉诺塔的移动步骤。