汉诺塔问题-递归实现-JAVA
1 public class hanio { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 //3层汉诺塔 A B C三个柱子 10 h(3, 'A', 'B', 'C'); 11 } 12 13 public static void h(int n,char a,char b,char c){ 14 if(n>0){ 15 //把n-1个盘子A放到C 16 h(n-1, a, c, b); 17 //n从A放到b 18 move(a,b); 19 //n-1在从C到B 20 h(n-1, c, b, a); 21 } 22 23 } 24 public static void move(char a,char b){ 25 System.out.println("从"+a+"移动到"+b); 26 } 27 28 }
本题采用递归实现
浙公网安备 33010602011771号