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);
}
}
如此复杂的问题可以用如此简单的代码实现。递归方法的最经典实例,一定要烂熟于心。
{
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);
}
}
如此复杂的问题可以用如此简单的代码实现。递归方法的最经典实例,一定要烂熟于心。
回复Comments
作者:
{commentrecontent}