汉诺塔实现:
用递归实现代码如下:
public class HanoTower {
public static void main(String[] args) {
Tower tower = new Tower();
tower.move(2,‘A’,‘B’,‘C’);
}
}
//使用递归实现汉诺塔
class Tower{
public void move(int num,char a,char b,char c){
if(num==1){
System.out.println(a+"–>"+c);
}else{
move(num-1,a,c,b);
System.out.println(a+"–>"+c);
move(num-1,b,a,c);
}
}
}