#include<stdio.h>

void hanoi(int n, char x, char y, char z);
void move(char x, char y);

int times = 0;	//表示移动圆盘的次数

void main()
{
	setvbuf(stdout, NULL, _IONBF, 0);    //使用eclipse开发环境时必须包含这个语句。不允许printf()函数输出到缓冲区,而是直接输出。
	
	int m;
	printf("input the number of disks:");
	scanf("%d", &m);
	printf("The step to moving %d diskes from A to C:\n", m);
	hanoi(m, 'A', 'B', 'C');	//将m个圆盘从A塔移动到C塔。
}

/* 
 * 将n个圆盘从x塔移动到z塔,y塔作为辅助塔
 */
void hanoi(int n, char x, char y, char z)
{
	if (n == 1)
		move(x, z);
	else
	{
		hanoi(n - 1, x, z, y);
		move(x, z);
		hanoi(n - 1, y, x, z);
	}
}

void move(char x, char y)
{
	printf("%d:\t%c-->%c\n",++times, x, y);
}

相关文章:

  • 2022-12-23
  • 2021-12-23
  • 2021-04-29
  • 2021-04-19
  • 2021-04-24
  • 2021-04-11
  • 2021-08-06
猜你喜欢
  • 2022-02-10
  • 2022-12-23
  • 2021-05-28
  • 2021-11-28
  • 2021-12-28
  • 2021-07-02
  • 2021-05-05
相关资源
相似解决方案