递归算法——Hannoi

#include 
#include 
using namespace std;

ofstream fout("hannoi.txt");

void Move(int n, char i, char j)
{
    fout << "put the " << n << "th from " << i << " to " << j << endl;
}

void Hannoi(int n, char a, char b, char c)
{
    if (1 == n)
    {
        Move(1, a, c);
    }
    else
    {
        Hannoi(n - 1, a, c, b);
        Move(n, a, c);
        Hannoi(n - 1, b, a, c);
    }
}

int main()
{
    fout << "the solution of 10 layers of hannoi:" << endl;
    Hannoi(10, 'a', 'b', 'c');
    fout.close();
    cout << "Done!" << endl;

    return 0;
}

你可能感兴趣的:(递归算法)