#include <iostream.h>
void move(char a,char c)
{
static long count=0;
count++;
cout<<"第"<<count<<"次:"<<a<<"->"<<c<<endl;
}
void hannuota(int num,char a,char b,char c)
{
if (num==1)
move(a,c);
else
{
hannuota(num-1,a,c,b);
move(a,c);
hannuota(num-1,b,a,c);
}
}
void main()
{
int num;
char a='a',b='b',c='c';
cout<<"请输入汉诺塔的层数:"<<endl;
cin>>num;
hannuota(num,a,b,c);
}
评论0