#include <stdio.h>
#include <stdlib.h>
//河内之塔 递归实现
void hanoi(int n,char A,char B,char C);
int main(int argc, char *argv[])
{
int n;
printf("输入盘数:");
scanf("%d",&n);
hanoi(n,'A','B','C');
system("PAUSE");
return 0;
}
void hanoi(int n,char A,char B,char C)
{
if(n==1)
{
printf("Move sheet %d from %c to %c/n",n,A,C);
}
else
{
hanoi(n-1,A,C,B);
printf("Move sheet %d from %c to %c/n",n,A,C);
hanoi(n-1,B,A,C);
}
}
本文介绍了一个使用C语言实现的河内塔问题解决方案。通过递归调用完成盘子从A柱移动到C柱的过程,B柱作为辅助。用户可以输入盘子的数量来观察移动步骤。
451

被折叠的 条评论
为什么被折叠?



