public class 汉诺塔 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
char from='A';
char depend='B';
char to='C';
int n=3;
汉诺塔.hanoi(n, from, depend, to);
}
private static int times=1;
public static void move(int n,char from,char to)
{
System.out.println("第"+(times++)+"步:把第"+n+"号盘子从"+from+"移到"+to);
}
public static void hanoi(int n,char from,char depend,char to)
{
if(n==1)
move(1,from,to);
else
{
hanoi(n-1,from,to,depend);
move(n,from,to);
hanoi(n-1,depend,from,to);
}
}
}
递归——汉诺塔问题
最新推荐文章于 2025-05-19 16:47:23 发布