汉诺塔 java实现

 1 public class Han {

 2     private int n = 3;

 3     private char a = 'a';

 4     private char b = 'b';

 5     private char c = 'c';

 6 

 7     public static void main(String[] args) {

 8         Han towerOfHanoi = new Han();

 9         System.out.println("木板个数" + towerOfHanoi.n);

10         towerOfHanoi.hanoi(towerOfHanoi.n, towerOfHanoi.a, towerOfHanoi.b,

11                 towerOfHanoi.c);

12     }

13 

14     private void hanoi(int n, char a, char b, char c) {

15         if (n > 0) {

16             this.hanoi(n - 1, a, c, b);

17             System.out.println("移动第"+n+"个,"+a + "->" + c);

18             this.hanoi(n - 1, b, a, c);

19         }

20     }

21 }

此汉诺塔算法非本人原创,收集于网络

你可能感兴趣的:(java实现)