1.河内塔:
package cn.starxing.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HeNeiTa {
public static void main(String[] args) {
int n;
BufferedReader buff;
buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入盘子个数:");
try {
n = Integer.parseInt(buff.readLine());
HeNeiTa hnt = new HeNeiTa();
hnt.move(n, 'A', 'B', 'C');
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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);
}
}
}
阅读(1133) | 评论(0) | 转发(0) |