# include <stdio.h>
int n;
int t = 0;
void move(char pos1, char pos2)
{
printf("%c -> %c\n", pos1, pos2);
}
void hanoi(int n, char pos1, char pos2, char pos3)
{
if (n == 1)
{
move(pos1, pos3);
t++;
}
else
{
hanoi(n-1, pos1, pos3, pos2);
move(pos1, pos3);
t++;
hanoi(n-1, pos2, pos1, pos3);
}
}
int main(){
printf("请输入有汉诺塔层数:");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
printf("次数为:%d\n", t);
return 0;
}
C语言汉诺塔问题-递归
最新推荐文章于 2024-07-25 23:20:14 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)