28 lines
691 B
Java
28 lines
691 B
Java
package Recursion;
|
|
|
|
import java.util.Scanner;
|
|
|
|
public class Hanoi {
|
|
|
|
private static Scanner sc = new Scanner(System.in);
|
|
public static void main(String[] args) {
|
|
System.out.println("Move how many disks from A to C?");
|
|
int n = sc.nextInt();
|
|
hanoi(n, "A", "B", "C");
|
|
|
|
}
|
|
|
|
private static void move(String from, String to) {
|
|
System.out.println("Move disc from "+from+" to "+to);
|
|
}
|
|
|
|
private static void hanoi(int number, String from, String helper, String to) {
|
|
if(number != 0) {
|
|
hanoi(number-1, from, to, helper);
|
|
move(from, to);
|
|
hanoi(number-1, helper, from, to);
|
|
}
|
|
|
|
}
|
|
|
|
} |