用c语言利用递归写一个汉诺塔游戏
时间: 2024-06-03 20:08:19 浏览: 150
汉诺塔非递归C语言实现.c
5星 · 资源好评率100%
#include <stdio.h>
void hanoi(int n, char from, char to, char temp) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", from, to);
return;
}
hanoi(n-1, from, temp, to);
printf("Move disk %d from %c to %c\n", n, from, to);
hanoi(n-1, temp, to, from);
}
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
hanoi(n, 'A', 'C', 'B');
return 0;
}
阅读全文