很典型的递归问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int i = 1;
void move(int n, char from, char to) {
cout<<"第"<<i++<<"步:将盘子从"<<from<<"移动到"<<to<<endl;
}
void hanoi(int n, char from, char by, char to) {
if (n == 1) {
move(1, from, to);
} else {
hanoi(n - 1, from, to, by);
move(1, from, to);
hanoi(n - 1, by, from, to);
}
}