#include<fstream>
using namespace std;
ifstream fin("h.in");
ofstream fout("h.out");
void hanoi(int n, char a, char b, char c,char t1[10], char t2[10], char t3[10])
{
if(n>=1)
{
hanoi(n-1,a,c,b,t1,t3,t2);
int x=strlen(t2);
t2[x]=t1[strlen(t1)-1];
t2[x+1]=0;
strcpy(t1+strlen(t1)-1, t1+strlen(t1));
fout<<t1<<endl<<t2<<endl<<t3<<endl;
fout<<a<<"->"<<b<<endl<<endl;
hanoi(n-1,c,b,a,t3,t2,t1);
}
}
int main()
{
char t1[10]="A", t2[10]="B", t3[10]="C";
int n;
fin>>n;
for(int i=n;i>=1;i--) t1[n-i+1]=i+'0';
t1[n+1]=0;
hanoi(n,'A','B','C',t1,t2,t3);
fin.close();
fout.close();
}
|