汉诺塔问题递归

汉诺塔是典型的递归问题
其思想是:n个盘子分为第n个盘子和前n-1个盘子,将前n-1个盘子先移动到C柱,将第n个盘子移动到B盘,最后再将前n-1个盘子移动到B盘(这一步是最后一步)
再将n-1个盘子递归就可以得出结果

import java.util.Scanner;

public class hanio汉诺塔 {
	static int step=1;
	static int count=0;
      public static void main(String arg[]) {
    	  Scanner sc=new Scanner(System.in);
    	  int n=sc.nextInt(); 
    	  hanio(n,'A','B','C');//将n从小到大摆放的盘子从A柱借助C柱移动到B柱
    	  System.out.println("total step:"+count);//需要的步数
      }

	private static void hanio(int n, char c, char d, char e) {
		if(n>0) {
			hanio(n-1,c,e,d);
			move(c,d);
			hanio(n-1,e,d,c);
		}
		
	}

	private static void move(char c, char d) {
		// TODO Auto-generated method stub
		System.out.println("Step "+step+" from "+c+" to "+d);//每次移动的步骤
		step++;
		count++;
		
	}
}
原文地址:https://www.cnblogs.com/hzcya1995/p/13309692.html