汉诺塔hanoi

#include<cstdio>

void hanoi(char A,char B,char C,int n)
{
    if(n==1)
    {
        printf("Move disk %d from %c to %c\n",n,A,C);
    }
    else
    {
        hanoi(A,C,B,n-1);
        printf("Move disk %d from %c to %c\n",n,A,C);
        hanoi(B,A,C,n-1);
    }
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        hanoi('A','B','C',n);
    }
}

你可能感兴趣的:(汉诺塔hanoi)