汉诺塔

汉诺塔递归

#include <bits/stdc++.h>
using namespace std;
int n;
/*
把 N- 1个从A经过C移动到B,最后一个从A到C
N- 1个在B上,经过A移动到C
*/
void hanuoti(int n,char A,char B,char C){
    if(!n)
        return;
    else{
        hanuoti(n - 1,A,C,B);
        //cnt++;记录移动的次数
        printf("Move disk %d from %c to %c
",n,A,C);
        hanuoti(n - 1,B,A,C);
    }
}

int main(){
    scanf("%d",&n);
    hanuoti(n,'A','B','C');
    return 0;
}





原文地址:https://www.cnblogs.com/xcfxcf/p/12301586.html