递归实现汉诺塔

分析

从A/B到C总共有两种可能,A->B->C或者B->A->C

代码

#include<stdio.h>
void hanio(int n,char A,char B,char C)
{
    if(n==1){
        hanio(n,A,B,C);
        printf("Move dist %d from %c to %c",n,A,C);
    }
    else{
        hanio(n-1,A,C,B);
        printf("Move dist %d from %c to %c",n,A,C);
        hanio(n-1,B,A,C);
}

int main()
{
    int n;
    printf("Input the order of Hanio tower:\n");
    scanf("%d",&n);
    hanio(n,'A','B','C');
    return 0;
}