+ -
当前位置:首页 → 问答吧 → 递归: 汉诺塔( 急 ! 栈)

递归: 汉诺塔( 急 ! 栈)

时间:2011-11-12

来源:互联网

void hanoi(int n,char one,char two,char three) // 定义hanoi函数  
  // 将n个盘从one座借助two座,移到three座  
 {
  void move(char x,char y); // 对move函数的声明  
  if(n==1)
  move(one,three);
  else
  {
  hanoi(n-1,one,three,two);
  move(one,three);
  hanoi(n-1,two,one,three);
  }
 }

 递归 hanoi(n-1,one,three,two);/这里有个问题,当hanoi(等于1时)不是直接输出了吗?
他怎么会执行?
move(one,1,three);
  hanoi(1,two,one,three);

作者: aliencool   发布时间: 2011-11-12

还有个问,第一个栈弹出后,他下面怎么会知道他弹出的值,而调用?计算机如何识别

作者: aliencool   发布时间: 2011-11-12

void hanoi(int n,char one,char two,char three){
if(n == 1){
 System.out.println("move " + n + "from " + one + "to " + three); 
}
else
{
hanoi(n-1,one,three,two);
System.out.println("move " + n + "from " + one + "to " + three); 
hanoi(n-1,two,one,three);
}
}

作者: keeya0416   发布时间: 2011-11-12

引用 2 楼 keeya0416 的回复:

void hanoi(int n,char one,char two,char three){
if(n == 1){
System.out.println("move " + n + "from " + one + "to " + three);
}
else
{
hanoi(n-1,one,three,two);
System.out.println("move " + n……


????

作者: aliencool   发布时间: 2011-11-12

热门下载

更多