Java汉诺塔递归算法

import java.io.*;

public class Ketang {
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());
Ketang ketang=new Ketang();
ketang.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);
}
}
}

posted @ 2016-10-12 11:14  小丑在嘲笑你  阅读(329)  评论(0编辑  收藏  举报