汉诺塔C语言实现(纯代码)

a、b、c三座塔,将n个从小到大(自上而下)的圆盘从a移动到c,移动期间小圆盘必须在大圆盘上面,问移动步骤。

#include<stdio.h>

int main() 
{
    void hanoi(int n,char one,char two,char three);
	int m;
	printf("请输入盘子数:");
	scanf("%d",&m);
	printf("移动%d个盘子的步骤是:\n",m);
	hanoi(m,'A','B','C');
	getchar();
	getchar();
}
void hanoi(int n,char one,char two,char three)
{
	void move(char x,char y);
	if(n==1)move(one,three);
	else
	{
		hanoi(n-1,one,three,two);
		move(one,three);
		hanoi(n-1,two,one,three);
	}
}
void move(char x,char y)
{
	printf("%c->%c\n",x,y);
}

效果如下:

猜你喜欢

转载自blog.csdn.net/li1593891525/article/details/80320980