汉诺塔递归次数为2^n-1,当n越大时所要移动的次数成指数急速增长
package 方法递归;
public class 汉诺塔递归 {
public static void main(String[] args) {
f(3,"A","B","C");
}
private static void f(int n,String x,String y,String z) {
if(n==1) {
System.out.println(x+"→"+z);
return; //一定要写返回,才能继续运行下去
}
f(n-1,x,z,y);
f(1,x,y,z);
f(n-1,y,x,z);
}
}