汉诺塔递归 @author wen
| 6 | * @author wen |
| 7 | */ |
| 8 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected