#include<stdio.h>
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);
return;
}
else
hanoi(n - 1, pos1, pos3, pos2);
move(pos1,pos3);
hanoi(n - 1, pos2, pos1, pos3);
}
int main()
{
int n = 0;
scanf_s("%d", &n);
hanoi(n,'A','B','C');
return 0;
}
汉诺塔递归
![](https://img-home.csdnimg.cn/images/20240711042549.png)