#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;
}
Hanoi(汉诺塔)问题递归实现
猜你喜欢
转载自blog.csdn.net/Nemoosi/article/details/104388994
今日推荐
周排行