initial commit
This commit is contained in:
28
Recursion/Hanoi.java
Normal file
28
Recursion/Hanoi.java
Normal file
@@ -0,0 +1,28 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user