递归经典--百练4147--汉诺塔问题

直接来看代码,来体会递归~

事实上,这个代码是最最让我体会到递归之美的~

好美啊~好妙啊~

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
using namespace std;

#define maxn 0x3f3f3f3f 

int coun=0;

void hanoi(int n,char a,char b,char c){            //将n个碟子,以b为中转,由a挪到c 
    if(n==1){
    cout<<1<<':'<<a<<"->"<<c<<endl;
    coun++;
    return;
    }
    hanoi(n-1,a,c,b);
    coun++;
    cout<<n<<':'<<a<<"->"<<c<<endl;
    hanoi(n-1,b,a,c);
    return;
}

int main(){
//    freopen("in.txt","r",stdin);
    int n;char a,b,c;
    scanf("%d %c %c %c",&n,&a,&b,&c);
    hanoi(n,a,b,c);
//    cout<<coun;
    return 0;
}
柳暗花明又一村
原文地址:https://www.cnblogs.com/ucandoit/p/8481021.html