class Hanoi { /* * Prints a sequence of moves to move n disks from peg `from' to * peg `to' using peg `temp' as a temporary peg. */ static void hanoi(int n, int from, int to, int temp) { if (n == 1) System.out.printf("%8d -> %2d%n", from, to); else { hanoi(n-1, from, temp, to); System.out.printf("%8d -> %2d%n", from, to); hanoi(n-1, temp, to, from); } } public static void main(String[] args) { int n = Integer.parseInt(args[0]); System.out.printf("Solution to hanoi(%d)%n", n); hanoi(n, 1, 2, 3); } }