package Hannuo;
import java.io.*;
public class Hanuoid {
private static int count = 0 ; //移动次数
public void move(int n, char Origin, char Destination, char Temporary){
if(n <= 0) {
System.out.println("The disk is illegal !") ;
return ;
}
if(n == 1){
count ++ ;
System.out.println("Move disk 1 from " + Origin + " to " + Destination + " " + count) ;
}
else {
move(n-1, Origin, Temporary, Destination) ;
count ++ ;
System.out.println("Move disk " + n + " from " + Origin + " to " + Destination + " " + count) ;
move(n-1, Temporary, Destination, Origin) ;
}
}
public static void main(String[] args){
Hanuoid hannuo = new Hanuoid() ;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
String input = "" ;
try{
input = br.readLine() ;
}catch(IOException e){
e.printStackTrace() ;
}
int num = Integer.parseInt(input) ;
hannuo.move(num, 'A', 'B', 'C') ;
}
}
汉诺塔————递归
最新推荐文章于 2022-02-25 23:47:59 发布