package none008第六章递归;
//汉诺塔问题,用递归
public class Tower {
/**
* @param args
*/
static int disk=3;
public static void main(String[] args) {
// TODO Auto-generated method stub
doTower(disk,'A','B','C');
}
public static void doTower(int topn,char from,char inter,char to){
if(topn==1)
System.out.println("Disk 1 from "+from+" to "+to);
else{
doTower(topn-1,from,to,inter);
System.out.println("Disk "+topn+" from "+from+" to "+to);
doTower(topn-1,inter,from,to);
}
}
}
递归:汉诺塔问题
最新推荐文章于 2024-11-21 22:37:20 发布