现在的位置: 首页 > 综合 > 正文

算法 – 汉诺塔问题(C++)

2014年05月18日 ⁄ 综合 ⁄ 共 304字 ⁄ 字号 评论关闭
#include <iostream>

using namespace std;

void print(char A, char C)
{
	cout << A << "-->" << C << endl;
}

void hanoi(int n, char A, char B, char C)
{
	if(n == 1)
	{
		print(A, C);
	}
	else
	{
		hanoi(n - 1, A, C, B);
		print(A, C);
		hanoi(n - 1, B, A, C);
	}
}

int main()
{
	int n;
	cout << "Please input the number of plates in pile A:";
	cin >> n;
	hanoi(n, 'A', 'B', 'C');
	system("pause");
	return 0;
}

抱歉!评论已关闭.