C++ - 汉诺塔

>=FreeMan=<2019-02-22 14:50:27
分类专栏:C++文章标签:汉诺塔C++
版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。
版权

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

/** Created by Chimomo*/#include <iostream>using namespace std;void print(char A, char C) {cout << A << " --> " << C << endl;}void hanoi(int n, char A, char B, char C) {if (n == 1) {print(A, C);} else {hanoi(n - 1, A, C, B);print(A, C);hanoi(n - 1, B, A, C);}}int main() {int n;cout << "Please input the number of plates in pile A:";cin >> n;hanoi(n, 'A', 'B', 'C');return 0;}// Output:/*Please input the number of plates in pile A:66A --> BA --> CB --> CA --> BC --> AC --> BA --> BA --> CB --> CB --> AC --> AB --> CA --> BA --> CB --> CA --> BC --> AC --> BA --> BC --> AB --> CB --> AC --> AC --> BA --> BA --> CB --> CA --> BC --> AC --> BA --> BA --> CB --> CB --> AC --> AB --> CA --> BA --> CB --> CB --> AC --> AC --> BA --> BC --> AB --> CB --> AC --> AB --> CA --> BA --> CB --> CA --> BC --> AC --> BA --> BA --> CB --> CB --> AC --> AB --> CA --> BA --> CB --> C*/
(0)

相关推荐