Online Compiler C

#include <stdio.h> void hanoi(int n, int from, int to, int temp) { printf("1"); } int main() { hanoi(2,1,1,2); return 0; }
这段代码定义了一个汉诺塔函数 `hanoi`,但函数体目前只打印数字1,没有实现真正的递归移动逻辑。主函数调用时参数设置也有问题。

- 你的 `hanoi` 函数目前只打印一次 "1",没有递归调用自身来移动盘子。汉诺塔的核心是:先把 n-1 个盘子从 from 移到 temp,再把第 n 个盘子从 from 移到 to,最后把 n-1 个盘子从 temp 移到 to。
- 主函数中调用 `hanoi(2,1,1,2)` 时,from 和 to 都是1,这不符合逻辑。汉诺塔需要三个不同的柱子,通常用1、2、3表示,from 和 to 应该不同。