public class Hanoi {
public static void main(String args[]) throws Exception {
int n;
BufferedReader buf =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入盘数:");
n = Integer.parseInt(buf.readLine());
Hanoi hanoi = new Hanoi();
hanoi.move(n, 'A', 'B', 'C');
}
public void move(int n, char a, char b, char c) {
if (n == 1)
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
else {
move(n - 1, a, c, b);
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
move(n - 1, b, a, c);
}
}
}作者: jlq 时间: 2015-10-21 00:26
so yyi de si作者: dai2686779 时间: 2015-10-21 00:36
6666666666666作者: 迷途老马 时间: 2015-10-24 21:53