t4_10.java
来自「多个java程序」· Java 代码 · 共 33 行
JAVA
33 行
//题目4.10
public class T4_10 {
private static int sumMoves=0;
public static void main(String[] args)
{
System.out.println("Enter number of Disks");
int n =MyInput.readInt();
System.out.println("The moves are:");
moveDisks(n,'A','B','C');
System.out.println("The sum of moves is "+sumMoves);
System.exit(0);
}
public static void moveDisks(int n ,char fromT,char toT,char auxT){
if(n==1){
System.out.println("move disk "+n+"from "+fromT+"to "+toT);
sumMoves++;
}
else{
moveDisks(n-1,fromT,auxT,toT);
System.out.println("move disk "+n+"from "+fromT+"to "+toT);
sumMoves++;
moveDisks(n-1,auxT,toT,fromT);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?