汉诺塔问题一直是我想解决的问题之一,汉诺塔也是让我感到编程的神奇之处,想了很久也领悟不了里面的奥秘。
附上灯哥教学视频https://www.bilibili.com/video/av9830115?from=search&seid=6226553387047262312
递归写法
public class Main { public void hanoi(int n, char a, char b, char c) { if (n == 1) { System.out.println(a + "->" + c); }else { hanoi(n-1,a,c,b); hanoi(1,a,b,c); hanoi(n-1,b,a,c); } } public static void main(String[] args) { Main m =new Main(); m.hanoi(3, 'a', 'b', 'c'); } }
n = N % M + N / M ;
来源:https://www.cnblogs.com/cznczai/p/11149790.html