MCPcopy Create free account
hub / github.com/CCCshengjiang/algorithm / Hanoi

Class Hanoi

data-structure-algorithm/src/cn/cwblue/dp/Hanoi.java:8–29  ·  view source on GitHub ↗

汉诺塔递归 @author wen

Source from the content-addressed store, hash-verified

6 * @author wen
7 */
8public class Hanoi {
9 public static void main(String[] args) {
10 hanoi(3);
11 }
12
13 public static void hanoi(int n) {
14 if (n == 0) {
15 return;
16 }
17 func(n, "left", "right", "mid");
18 }
19
20 private static void func(int n, String left, String right, String other) {
21 if (n == 1) {
22 System.out.println("move " + n + " from " + left + " to " + right);
23 return;
24 }
25 func(n - 1, left, other, right);
26 System.out.println("move " + n + " from " + left + " to " + right);
27 func(n - 1, other, right, left);
28 }
29}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected