public class Hanoif {
// 汉诺塔
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("请输入盘子数: ");
int n = sc.nextInt();
System.out.println("需要移动的次数: " + hanoif(n, 'A', 'B', 'C'));
}
// hanoif方法
static int hanoif(int n, char A, char B, char C) {
int re = 0;
if (n == 0)
return 0;
re += hanoif(n - 1, A, C, B);
;
re += 1;
System.out.println(n + ": " + A + "-->" + B);
re += hanoif(n - 1, C, B, A);
return re;
}
}
Hanoif汉诺塔
最新推荐文章于 2022-09-05 16:01:58 发布