运用递归实现汉诺塔
# include<stdio.h>
void hannuota(int n, char A, char B, char C)
{
if(1 == n)
printf("将编号为%d的盘子从%c柱子上移动到%c的柱子上\n", n, A, C);
else
{
hannuota(n-1, A, C, B);
printf("将编号为%d的盘子从%c柱子上移动到%c的柱子上\n", n, A, C);
hannuota(n-1, B, A, C);
}
return;
}
int main(void)
{
int n;
char A = 'A';
char B = 'B';
char C = 'C';
printf("请输入盘子的个数:");
scanf("%d", &n);
hannuota(n, A, B, C);
return 0;
}
如果输入为3,则输出结果为