自己写了一个河内塔程序

来源:互联网 发布:能以礼让为国乎赏析 编辑:程序博客网 时间:2024/05/10 20:29


public class Hanoi {

public static void main(String[] args) {
hanoi(3,"A","B","C");

}


public static void hanoi(int n,String from,String by,String to){
if(n==1){
System.out.println(from +"   --->   " + to);
}else{
hanoi(n-1,from,to,by);
System.out.println(from +"   --->   " + to);
hanoi(n-1,by,from,to);
}
}

}
原创粉丝点击