汉诺塔C++实现,递归调用。
#include<iostream>
using namespace std;
void hanoi(int n, char one, char two, char three); //Hanoi move
void move(char x, char y); //move step after
int main()
{
int m;
cout << "please input num: ";
cin >> m;
hanoi(m,'A','B','C');
return 0;
}
//Hanoi move
void hanoi(int n, char one, char two, char three)
{
if(n == 1)
move(one, three);
else
{
hanoi(n-1, one, three, two);
move(one, three);
hanoi(n-1, two, one, three);
}
}
//move step after
void move(char x, char y)
{
cout << x << "-->" << y << endl;
}