| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 421 人关注过本帖
标题:哪位高手帮我一下
只看楼主 加入收藏
A258
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-3-13
收藏
 问题点数:0 回复次数:1 
哪位高手帮我一下
int i=0;
void main()
{  
    int m;
    cin>>m;
    cout<<"buzhou:";
    cout<<endl;
    hanoi(m,'A','B','C');
    out();
}
 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);
    }
}
void move( char x, char y )
{
    cout<<"从"<< x<<"到"<<y ;
    i++;
    cout<<endl;
}
void out()
{
    cout<<"总共要搬"<<i<<"次";
    cout<<endl;
}
 
哪位高手帮我一下
谢谢了
其中的void hanoi(int n,char one, char two, char three) ;函数到底是怎样运行的啊
要详细的解答
2008-03-13 18:49
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
这是分治法的思路#include<iostream.h>
int i=0;
void hanoi(int,char,char,char);
void move(char,char);
void out();
void main()
{  
    int m;
    cin>>m;
    cout<<"buzhou:";
    cout<<endl;
    hanoi(m,'A','B','C');
    out();
}
void hanoi(int n,char one, char two, char three)
{   cout<<one<<two<<three<<endl;
    if(n==1)
    move(one,three);
    else
    {
    hanoi(n-1,one,three,two); //一通过三搬到二
    move(one,three); //把一搬到三
    hanoi(n-1,two,one,three); //把二上面的通过一搬到三
    }
}
void move( char x, char y )
{
    cout<<"从"<< x<<"到"<<y ;
    i++;
    cout<<endl;
}
void out()
{
    cout<<"总共要搬"<<i<<"次";
    cout<<endl;
}
。。。要向搬最后一个假定前面的n-1个已经搬了。。。

学习需要安静。。海盗要重新来过。。
2008-03-13 19:15
快速回复:哪位高手帮我一下
数据加载中...
 
   



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

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