汉诺塔问题

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

int move()
{
    static int step;
    step++;
};

int hanoi(int n)
{
    if(n==1)
       return move();
    else
    {

        n--;
        hanoi(n);
        move();
        hanoi(n);
    }
};

int main()
{
    int number;
    printf("Please enter the number of the plates:");
    scanf("%d",&number);
    int k=hanoi(number);
    printf("the step is:%d",k);
    return 0;
}

汉诺塔问题可以分解为

1.当n=1的时候,A B C直接从A--C

2.n=2的时候,A-B A-C B-C

3.将盘子分解为上面的n-1个和最下面的一个 上面的n-1个可以借助C 移到B 从而又是一个汉诺塔问题 hanoi(n-1),最下面的一个大盘子A-C n-1个盘子借助A移到C hanoi(n-1);

4.return

 

//但是我的程序在n=64时没有办法运算出结果 超出int的范围,暂时不知道怎么解决

posted @ 2013-03-10 10:41  天晴会下雨  阅读(135)  评论(0)    收藏  举报