算法:Java 版 汉诺塔

Jul 08 2010

算法:Java 版 汉诺塔

package test;
 
public class Tower {
 
	public static void main(String[] args)throws Exception{
		move(4,'A','B','C');
	}
 
	public static void move(int n,char from,char to,char mid){
		if(n==1){
			System.out.println("把盘子"+n+"从"+from+"移动到"+to);
			return;
		}
		move(n-1,from,mid,to);
		System.out.println("把盘子"+n+"从"+from+"移动到"+to);
		move(n-1,mid,to,from);
	}
 
}
Tags:

No responses yet

Leave a Reply