汉诺塔

      专业 2005-7-12 17:39
public class hanoitower
{
public static void move(int n,char from,char to,char aux)
{
if (n==1)
{
System.out.println("move plate ◎1 from "+from+" to "+to);
//i++;
}
else
{
move(n-1,from,aux,to);
System.out.println("move plate ◎"+n+" from "+from+" to "+to);
//i++;
move(n-1,aux,to,from);
}
}

public static void main(String args[])
{
move(9,'A','C','B');
//System.out.println("i="+i);
}
}

如此复杂的问题可以用如此简单的代码实现。递归方法的最经典实例,一定要烂熟于心。
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}