Hanoi(汉诺塔)问题递归实现

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
void Hanoi(int n, string A, string B, string C){
	if(n == 1)
		cout << "Move top disk from peg " << A << " to peg " << C << endl;
	else{
		Hanoi(n-1, A, C, B);
		cout << "Move top disk from peg " << A << " to peg " << C << endl;
		Hanoi(n-1, B, A, C);
	}
}
int main(){
	string a = "a";
	string b = "b";
	string c = "c";
	Hanoi(3, a, b, c);
	return 0;
}
发布了13 篇原创文章 · 获赞 0 · 访问量 444

猜你喜欢

转载自blog.csdn.net/Nemoosi/article/details/104388994