汉诺塔问题

来源:互联网 发布:企业免费记账软件 编辑:程序博客网 时间:2024/05/16 13:50

1.matlab代码

function [ ] = hanno( n ,A,B,C)%n表示层数,A开始的第1根,B为辅助第2根,C目标柱子第3根%   formatSpec = '%d -->%d.';if n>=1    hanno(n-1,A,C,B);    step=sprintf(formatSpec,A,C)    hanno(n-1,B,A,C);endend

2.运行结果

>>  hanno( 2,1,2,3 )step =1 -->2.step =1 -->3.step =2 -->3.>>  hanno( 3,1,2,3 )step =1 -->3.step =1 -->2.step =3 -->2.step =1 -->3.step =2 -->1.step =2 -->3.step =1 -->3.
原创粉丝点击