-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHanoi.java
More file actions
41 lines (38 loc) · 705 Bytes
/
Hanoi.java
File metadata and controls
41 lines (38 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @date 20150630
* @author ywang
* @http http://baike.baidu.com/view/191666.htm
* @param n 层数
* @param A B C 三根柱子名称
*
*/
package com.classicalproblem;
public class Hanoi
{
/*
int n; //汉诺塔层数
public Hanoi(int n)
{
this.n = n;
}*/
public void hanoi(int n, char A, char B, char C)
{
if (n == 1)
{
System.out.println("Move: " + A + " --> " + C);
}
else
{
hanoi(n-1, A, C, B);
System.out.println("Move: " + A + " --> " + C);
hanoi(n-1, B, A, C);
}
}
public static void main(String[] args)
{
//Hanoi hanoi = new Hanoi(1);
//hanoi.hanoi('A', 'B', 'C');
Hanoi hanoi2 = new Hanoi();
hanoi2.hanoi(3, 'A', 'B', 'C');
}
}