#include<stdio.h>
#include<iostream.h>
class hanio{
public:
int n; //n代表盘子的个数
long count; //count返回move函数调用的次数,即反映出算法时间复杂度问题的参量
char from,to,use; //from,to,use是源桩,目标桩,借用桩
void move(int n,char from, char to); //将第n个盘子从from桩移动到to桩上
void change(int n,char from,char use,char to);
void solve();
void set_n(int);
int set_n();
long getCount();
hanio(int n, char from ,char use, char to); //带盘子个数n和源桩、目标桩、借用桩的构造函数
hanio(int n); //只带盘子个数的构造函数
};
int hanio::set_n()
{
cout<<endl<<"enter a number:";
cin>>n;
cout<<endl;
return n;
}
long hanio::getCount()
{
count =0;
return count;
}
/*void hanio::set_n(int num)
{
n=num;
}
*/
hanio::hanio(int n, char a, char b, char c)
{
//n=num;
from=a;
use=b;
to=c;
}
void hanio::move(int n, char from, char to)
{
count++;
cout<<"the "<<count <<"time to call 'move' function"<<endl;
//cout<<"::::: move "<<n<<"from "<<from<<"to "<<to<<endl;
}
void hanio::change(int n, char from, char use,char to)
{
if(n>0)
{
change(n-1,from,to,use);
move(n,from,to);
change(n-1,use,from,to);
}
}
void hanio::solve()
{
change(n,from,use,to);
}
void main(void)
{
hanio ob(5,'A','B','C');
ob.solve();
ob.set_n();
ob.solve();
return ;
}
评论0