汉诺塔matlab实现

Matlab的递归调用,好久不用matlab了,练练手。

 

global handCount;

handCount = 1;

huuotsun(1, 2, 3, 3)

 

 

function huuotsun(cur, buf, tar, num)

    global handCount;

    if num == 1

        str = sprintf('c %d plate  %d to %d ', handCount, cur, tar);

        disp(str);

        handCount = handCount + 1;

    else

        huuotsun(cur, tar,buf, num -1);

        str = sprintf('c %d plate  %d to %d ', handCount, cur, tar);

        disp(str);

        handCount = handCount + 1;

        huuotsun(buf, cur,tar, num -1);

    end

end

 

 

posted @ 2015-10-06 18:13  guopengfei  阅读(1358)  评论(0编辑  收藏  举报