#include<iostream>
using namespace std;

int times = 0;

void move(char src, char dst)
{
	times++;
	cout << times << ": " << src << "--->" << dst << endl;
}

void hanNuoTower(int n, char first, char mid, char last)
{
	if(1 == n) //递归出口不可忘
		move(first, last);
	else
	{
		hanNuoTower(n - 1, first, last, mid);
		move(first, last);
		hanNuoTower(n - 1, mid, first, last);
	}
}

int main()
{
	int n = 7;
	hanNuoTower(n, 'A', 'B', 'C');

    return 0;
}
 


本文转载:CSDN博客