汉诺塔问题 java源码
时间: 2023-11-16 20:07:50 浏览: 98
汉诺塔课程设计
以下是汉诺塔问题的Java源码实现:
```java
public class HanoiTower {
public static void main(String[] args) {
int nDisks = 3;
doTowers(nDisks, 'A', 'B', 'C');
}
public static void doTowers(int topN, char from, char inter, char to) {
if (topN == 1) {
System.out.println("Disk 1 from " + from + " to " + to);
} else {
doTowers(topN - 1, from, to, inter);
System.out.println("Disk " + topN + " from " + from + " to " + to);
doTowers(topN - 1, inter, from, to);
}
}
}
```
阅读全文