#include<cstdlib>
#include<fstream>
#include<iostream>
using namespace std;
ofstream fout("out.txt");
void Move(int n,char i,char j)
{
fout<<"把"<<n<<"从"<<i<<"移动到"<<j<<"上面"<<endl;
}
void Hannoi(int n,char a,char b,char c)
{
if (n==1)
{
Move(1,a,c);
}
else
{
Hannoi(n-1,a,c,b);
Move(n,a,c);
Hannoi(n-1,b,a,c);
}
}
int main(int argc,char *argv[])
{
fout<<"以下是10层汉诺塔的解法:"<<endl;
Hannoi(2,'a','b','c');
fout.close();
cout<<"输出结果完毕"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
2013年3月算法练习(三) 汉诺塔递归
最新推荐文章于 2020-06-11 15:25:00 发布