ITeye博客

C++用递归解决汉诺塔问题(续)

yunchow · 2009-05-19 12:16 · C++ · 编程语言 · C · C++ · C# · Markdown


#include <iostream.h>
using namespace std;

void move(int top,int n,char src,char dst,char tmp)
{
	if(1==n)	
		cout << top << ":" << src << "-->" << dst << endl;
	else
	{
		move(top,n-1,src,tmp,dst);
		move(top+n-1,1,src,dst,tmp);
		move(top,n-1,tmp,dst,src);
	}
}
int main(void)
{
	move(1,3,'A','B','C');
	return 0;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。