使用函数的递归调用来解决Hanoi(汉诺)塔问题。

#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:
", 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:	%c-->%c
",++times, x, y);
}
原文地址:https://www.cnblogs.com/Camilo/p/3338418.html