下面是一个C语言的汉诺塔递归算法代码示例:
#include <stdio.h>void hanoi(int n, char from, char to, char aux) { if (n == 1) { printf("Move disk 1 from %c to %c\n", from, to); return; } hanoi(n-1, from, aux, to); printf("Move disk %d from %c to %c\n", n, from, to); hanoi(n-1, aux, to, from);}int main() { int n; printf("Enter the number of disks: "); scanf("%d", &n); hanoi(n, 'A', 'C', 'B'); return 0;}以上代码使用递归的方式实现了汉诺塔问题的解决方法。函数hanoi接受参数n表示当前要移动的盘子数量,from表示起始柱子,to表示目标柱子,aux表示辅助柱子。在每次递归中,将n-1个盘子从起始柱移动到辅助柱,然后将最后一个盘子从起始柱移动到目标柱,最后将n-1个盘子从辅助柱移动到目标柱。主函数中输入盘子数量并调用hanoi函数进行求解。


