class Hanoi { static int numSteps; /** * Prints an instruction to move a disk from peg `from' to peg `to'. */ static void print1move(char from, char to) { System.out.printf("Step %d: move the topmost disk of peg %c to peg %c%n", ++numSteps, from, to); } /** * Prints a sequence of instructions to move n disks from peg `from' to * peg `to' using peg `temp' as a temporary peg. */ static void hanoi(int n, char from, char to, char temp) { if (n > 1) hanoi(n-1, from, temp, to); print1move(from, to); if (n > 1) hanoi(n-1, temp, to, from); } public static void main(String[] args) { int n = Integer.parseInt(args[0]); System.out.printf("%nSolution to hanoi(%d)%n=====================%n%n", n); hanoi(n, 'A', 'B', 'C'); System.out.println(); } }