java 汉诺塔问题

来源:互联网 发布:google chrome mac下载 编辑:程序博客网 时间:2024/06/05 11:44

汉诺塔:

package HanoTower;/** * 目的:A->C,B是中介 *  * 思想:想把n 从A->C,需要把n-1从A->B,再把n-1从B->C,中间将N放在C * 比如将2从A->C,那么将1放在B,(将2放在C),将1放在C, * @author jalo * */public class HanoTower {public static void moveDish(int dish, char from, char inter, char to) {if(dish == 1)System.out.println("将盘子 "+ dish + " 由 " + from + " 移动到 " + to);else {moveDish(dish-1, from, to, inter);System.out.println("将盘子 "+ dish + " 由 " + from + " 移动到 " + to);moveDish(dish-1, inter, from, to);}}    public static void main(String[] args) {      int nDishes = 3;//最大盘子号,从1开始    System.out.println("begin");    moveDish(nDishes,'A','B','C');    System.out.println("end");    }  }


0 0
原创粉丝点击