1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class { static int i=0; public static void main(String[] args) { hanoi(20,"一号塔","二号塔","三号塔"); } private static void hanoi(int n, String s, String m, String e) { if (n==1){ move(s,1,e); }else { hanoi(n-1,s,e,m); move(s, n, e); hanoi(n-1,m,s,e); } } static void move(String s, int n, String e){ i++; System.out.println("将编号为"+n+"的盘子,从"+s+"移动到"+e+"移动了"+i+"次"); } }
|
近期评论