递归补充---hanoi塔

hanoi

#include "iostream"
using namespace std;

void move(char sta,char des)
{
    
    
    cout<<sta<<">>"<<des<<endl;
}

void hanoi(int n,char sta,char mid,char des)
{
    
    
    if(n==1)
        move(sta,des);
    else
    {
    
    
        hanoi(n-1,sta,des,mid);
        
        move(sta,des);
        
        hanoi(n-1,mid,sta,des);
    }
}

int main()
{
    
    
    int m;
    cout << "Enter the number of diskes:";
    
    cin >> m;
    
    cout<<"the steps to moving "<< m <<"diskes:"<<endl;
    
    hanoi(m,'A','B','C');
    
    return 0;
}

Guess you like

Origin blog.csdn.net/niko02/article/details/121469702