汉诺塔linux电脑编程结果截图,汉诺塔编程问题!

满意答案

00e27ab806e4881f8254fe7ae8741834.png

公元1699年

推荐于 2018.04.05

00e27ab806e4881f8254fe7ae8741834.png

采纳率:46%    等级:12

已帮助:10767人

正确的程序应5261该这样写

void hanoi( int n,char one ,char two,char three)

{

if( n==1 )

{

move( one,three);

}

else

{

hanoi( n-1, one, three, two);

move( one, three);

hanoi( n-1, two, one, three);

}

}

解释:

将 n 个塔牌从 one 经过 two 全部移动4102到 three

{

如果 n == 1 那么

{

将那个塔牌从 one 移动到 three;

任务完成!

}

否则

{// 就至少需1653要三步了

将 n-1 个塔牌从 one 经过 three 全部移动到 two // 此过程结束后,塔牌数量为 [1, n-1, 0]

将 one 移动到 three // 过过程结束后,塔牌数量为 [0, n-1, 1]

将 n-1 个塔牌从 two 经过 one 全部移动到 three // 过过程结束后,塔牌数量为 [0, 0, n]

}

}

通过 n=3 来跟踪,展开会好理解些,下面就是执行过程的展开,// 后面会用{}来展开,不要怕难,使用文字缩进,跟踪读下去:

hanoi(3, one, two,three)

{

//hanoi( 2, one, three, two);

{

//hanoi( 1, one, two, three);

{

move( one, three);

}

move( one, two);

//hanoi( 1, three, one, two);

{

move( three, two);

}

}

move( one, three);

//hanoi( 2, two, one, three);

{

//hanoi( 1, two, three, one);

{

move( two, one);

}

move( one, two);

//hanoi( 1, one, two, three);

{

move( one, three);

}

}

}

20分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值