首页 > 代码库 > 汉诺塔问题

汉诺塔问题

模拟汉诺塔的移动过程,CC答案的面向对象的太烦,写个以前学的经典的。

public class Solution {    public void move(int n, char a, char b, char c) {        if (n == 1) {            System.out.println("move from " + a + " to " + c);            return;        }        move(n - 1, a, c, b);        move(1, a, b, c);        move(n - 1, b, a, c);    }    public static void main(String[] args) {        new Solution().move(3, ‘a‘, ‘b‘, ‘c‘);    }}