递归算法——Hannoi
来源:互联网 发布:行业差距 知乎 编辑:程序博客网 时间:2024/06/05 03:11
#include <iostream>#include <fstream>using namespace std;ofstream fout("hannoi.txt");void Move(int n, char i, char j){ fout << "put the " << n << "th from " << i << " to " << j << endl;}void Hannoi(int n, char a, char b, char c){ if (1 == n) { Move(1, a, c); } else { Hannoi(n - 1, a, c, b); Move(n, a, c); Hannoi(n - 1, b, a, c); }}int main(){ fout << "the solution of 10 layers of hannoi:" << endl; Hannoi(10, 'a', 'b', 'c'); fout.close(); cout << "Done!" << endl; return 0;}