汉诺塔问题

思路:从A移动到B,从B移动到C(总体思路)

#include <stdio.h>
#include <stdlib.h>

void Move(int n,char x,char y)
{
    printf("move %d from %c to %c\n",n,x,y);
}

void Hannoi(int n,char a,char b,char c)
{
    if(n==1)
    {
        Move(1,a,c);
    }
    else
    {
        Hannoi(n-1,a,c,b);
        Move(n,a,c);
        Hannoi(n-1,b,a,c);
    }
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        Hannoi(n,'a','b','c');
    }
    return 0;
}


posted @ 2014-10-27 16:01  静以养身 俭以养德  阅读(123)  评论(0编辑  收藏  举报