Skip to content

Commit 504612c

Browse files
author
タイン・フイ
authored
Create HanoiTower.java
1 parent 959eda9 commit 504612c

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

HanoiTower.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class MainClass {
2+
public static void main(String[] args) {
3+
int nDisks = 3;
4+
doTowers(nDisks, 'A', 'B', 'C');
5+
}
6+
public static void doTowers(int topN, char from, char inter, char to) {
7+
if (topN == 1) {
8+
System.out.println("Disk 1 from " + from + " to " + to);
9+
} else {
10+
doTowers(topN - 1, from, to, inter);
11+
System.out.println("Disk " + topN + " from " + from + " to " + to);
12+
doTowers(topN - 1, inter, from, to);
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)