package fa.ct; import java.util.Scanner; public class Hanoi { public static void hanoi(int num,char p1,char p2,char p3){
if(num==1){
move(p1,1,p3);
}
else{
hanoi(num-1,p1,p3,p2);
move(p1,num,p3);
hanoi(num-1,p2,p1,p3);
}
}
public static void move(char ps,int num,char pt){
System.out.println("把"+num+"号盘子,从第"+ps+"号柱子移到第"+pt+"号柱子上");
}
public static void start(){
System.out.println("Please input the number of the pies:");
Scanner scann=new Scanner(System.in);
int num=scann.nextInt();
hanoi(num,'1','2','3');
}
public static void main(String[] args) {
Hanoi hanoi;
System.out.println("通过递归实现Hanoi问题:");
Hanoi.start();
} }