简单入门程序:汉诺塔

#include <iostream>
#include <string>

using namespace std;

void Hanoi(int n, char a, char b, char c)
{
	if (n == 1)
	{
		cout << a << "--->" << c << endl;
	}
	else
	{
		Hanoi(n - 1, a, c, b);
		cout << a << "--->" << c << endl;
		Hanoi(n - 1, b, a, c);
	}
}

int main(int argc, char *argv[])
{
	cout << "请输入要移动的数目:";
	int n;
	cin >> n;
	cout << endl << "移动过程如下:" << endl << endl;
	Hanoi(n, 'A', 'B', 'C');
	system("pause");
	return 0;
}

你可能感兴趣的:(简单入门程序:汉诺塔)