⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hannoi.java

📁 JAVA编程思想源代码 值得一下 很难找的
💻 JAVA
字号:
package chapter6;

public class Hannoi {
	private static int totalSteps = 0;

	public static void hannio(int plates, String A, String B, String C) {

		//A为起始杆,B为临时杆,C为目标杆
		if (plates == 1) {
			System.out.println("Step:" + A + "-->" + C);
			totalSteps++;

		} else if (plates > 1) {
			hannio(plates - 1, A, C, B);
			System.out.println("Step:" + A + "-->" + C);

			hannio(plates - 1, B, A, C);
			totalSteps++;

		}
	}

	public static void main(String args[]) {
		
		int plates = 3;
		
		String A = "A", B = "B", C = "C";

		System.out.println("the plates number is " + plates);
		System.out.println("Hannoi 's method path is ");

		hannio(plates, A, B, C);
		//输出总共移动的步数
		System.out.println("Total steps is " + totalSteps);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -