| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 943 人关注过本帖
标题:求助!!!汉诺塔递归怎么实现调用的,不明白
只看楼主 加入收藏
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
结帖率:97.5%
收藏
已结贴  问题点数:20 回复次数:2 
求助!!!汉诺塔递归怎么实现调用的,不明白
看了2天还是不明白,不是n==1是递归结束条件嘛,从4步后的调用怎么来的,带图。请详解,谢谢!!!

图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
#include<stdio.h>
#include<math.h>
int main()
{
    int a;
    void hanoi(int m,char one,char two,char three);
    while(scanf("%d",&a)!=EOF)
    {
    int t=pow(2,a)-1;
    printf("移动%d块盘子共有%d个步骤,如下\n",a,t);
     hanoi(a,'A','B','C');
    }
    return 0;
}
void hanoi(int n,char one,char two,char three)
{
    void move( int b,char x,char y);
    if(n==1)
    move(n,one,three);
    else
    { 
        hanoi(n-1,one,three,two);
        move(n,one,three);
        hanoi(n-1,two,one,three);
    }
}
void move(int b,char x,char y)
{
    printf("%d %c-->%c\n",b,x,y);
}



[此贴子已经被作者于2017-3-7 16:58编辑过]

2017-03-07 14:33
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:14 
回复 楼主 Emotiona
#include<stdio.h>        
void hanoi(int N,char A,char B,char C)        
{        
    if(N==1)/*将A座上剩下的第N个盘子移动到C座上*/   
        printf("move dish %d from %c to %c\n",N,A,C);/*打印移动步骤*/
    else   
    {   
        hanoi(N-1,A,C,B);/*借助C座将N-1个盘子从A座移到B座*/
        printf("move dish %d from %c to %c\n",N,A,C);/*打印移动步骤*/
        hanoi(N-1,B,A,C);/*借助A座将N-1个盘子从B座移到C座*/
    }   
}        
main()        
{        
    int n;   
    printf("Please input the nuber of dishes;");   
    scanf("%d",&n);/*输入要移动的盘子个数*/   
    printf("The steps to move %2d dishes are:\n",n);   
    hanoi(n,'A','B','C');/*调用递归函数*/   
}        


Please input the nuber of dishes;3
The steps to move  3 dishes are:
move dish 1 from A to C
move dish 2 from A to B
move dish 1 from C to B
move dish 3 from A to C
move dish 1 from B to A
move dish 2 from B to C
move dish 1 from A to C
Press any key to continue

Please input the nuber of dishes;4
The steps to move  4 dishes are:
move dish 1 from A to B
move dish 2 from A to C
move dish 1 from B to C
move dish 3 from A to B
move dish 1 from C to A
move dish 2 from C to B
move dish 1 from A to B
move dish 4 from A to C
move dish 1 from B to C
move dish 2 from B to A
move dish 1 from C to A
move dish 3 from B to C
move dish 1 from A to B
move dish 2 from A to C
move dish 1 from B to C
Press any key to continue
2017-03-07 18:55
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
收藏
得分:0 
回复 2楼 宇宙规律
hanoi(N-1,A,C,B);/*借助C座将N-1个盘子从A座移到B座*/
        printf("move dish %d from %c to %c\n",N,A,C);/*打印移动步骤*/
        hanoi(N-1,B,A,C);/*借助A座将N-1个盘子从B座移到C座*/

我是不明白 整个递归的调用  比如代值3进去  怎么也出不来程序结果。程序意思都明白。
(2,A,B,C)(1,A,C,B)   ...MAX(A,C)  ...(2,A,B,C)(1,B,A,C)
其他的不明白怎么调用的,麻烦解答下!
2017-03-07 22:35
快速回复:求助!!!汉诺塔递归怎么实现调用的,不明白
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018004 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved