聰明的你還有計算機幫你完成,你能寫一個程序幫助僧侶們完成這輩子的夙愿嗎? Input 輸入金片的個數n。這里的n<=10。 Output 輸出搬動金片的全過程。格式見樣例。 Sample Input 2 Output Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C Hint 可以用遞歸算法實現。
import java.util.Scanner;publicclassMain{publicstaticvoidMove(int n,char getone,char putone){System.out.println("Move disk "+ n +" from "+ getone +" to "+ putone);}publicstaticvoidHanio(int n,char one,char two,char three){if(n ==1)Move(n, one, three);else{Hanio(n -1, one, three, two);Move(n, one, three);Hanio(n -1, two, one, three);}}publicstaticvoidmain(String[] args){Scanner reader =newScanner(System.in);int n = reader.nextInt();Hanio(n,'A','B','C');reader.close();}}