直接参考了 清华大学 严蔚敏 数据结构C语言描述
/**/
/*
* 文件名:hanoi.cpp
* 功能:演示汉诺塔程序
* 创建日期:2007-10-15
*/
#include < iostream >
using namespace std;
int c;
void move( char x, int n, char y)
... {
cout << ++c << " " << "Move " << n << " from " << x << " To " << y << endl;
}
void hanoi( int n, char A, char B, char C)
... {
if (n == 1)
move(A, 1, C);
else
...{
hanoi(n - 1, A, C, B);
move(A, n, C);
hanoi(n - 1, B, A, C);
}
}
int main()
... {
c = 0;
int n;
cout << "Please input n : " ;
cin >> n;
hanoi(n, 'A', 'B', 'C');
return 0;
}
* 文件名:hanoi.cpp
* 功能:演示汉诺塔程序
* 创建日期:2007-10-15
*/
#include < iostream >
using namespace std;
int c;
void move( char x, int n, char y)
... {
cout << ++c << " " << "Move " << n << " from " << x << " To " << y << endl;
}
void hanoi( int n, char A, char B, char C)
... {
if (n == 1)
move(A, 1, C);
else
...{
hanoi(n - 1, A, C, B);
move(A, n, C);
hanoi(n - 1, B, A, C);
}
}
int main()
... {
c = 0;
int n;
cout << "Please input n : " ;
cin >> n;
hanoi(n, 'A', 'B', 'C');
return 0;
}