酷代码 AI
菜单
服务商

上题运行错误,请重写

最少的移动次数为 \( 2^N - 1 \)。 移动步骤如下: ```python def hanoi(n, source, target, auxiliary, moves): if n == 1: moves.append(f"{source}->{n}->{target}") else: hanoi(n - 1, source, auxiliary, target, moves) moves.append(f"{source}->{n}->{target}") hanoi(n - 1, auxiliary, target, source, moves) def main(): N = int(input().strip()) moves = [] hanoi(N, 'a', 'c', 'b', moves) print(2 N - 1) for move in moves: print(move) if __name__ == "__main__": main() ``` 输入样例: ``` 2 ``` 输出: ``` 3 a->1->b a->2->c b->1->c ``` [2026-01-06 12:33:01 | AI问答 | 202点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多