一下代码希望能帮我找找错误 , 谢谢啦。。
#include<iostream.h>
int step=1;
void move(int,char,char,char);
void main()
{
char a,b,c;
int n;
cout<<"请输入盘数n="<<endl;
cin>>n;
cout<<"在三根柱子上移"<<n<<"只盘子的步骤为"<<endl;
move(n,a,b,c);
}
void move(int m,char p,char q,char r)
{
if (m==1)
{
cout<<"["<<step<<"]move 1# from"<<p<<"to"<<r<<endl;
step++;
}
else
{
move(m-1,p,r,q);
cout<<"["<<step<<"]move"<<m<<"# from"<<p<<"to"<<r<<endl;
step++;
move(m-1,q,p,r);
}
}
#include<iostream.h>
int step=1;
void move(int,char,char,char);
void main()
{
char a='a',b='b',c='c';
int n;
cout<<"请输入盘数n="<<endl;
cin>>n;
cout<<"在三根柱子上移"<<n<<"只盘子的步骤为"<<endl;
move(n,a,b,c);
}
void move(int m,char p,char q,char r)
{
if (m==1)
{
cout<<"["<<step<<"]move 1# from "<<p<<" to "<<r<<endl;
step++;
}
else
{
move(m-1,p,r,q);
cout<<"["<<step<<"]move"<<m<<"# from "<<p<<" to "<<r<<endl;
step++;
move(m-1,q,p,r);
}
}
在MOVE函数就将p q r;初始化一下,就是标记一下。如p='A';q='B',r='C';如果不初始化编译器就不知道就会乱分配。
#include<iostream.h>
int step=1;
void move(int,char,char,char);
void main()
{
char a,b,c;
int n;
cout<<"请输入盘数n="<<endl;
cin>>n;
cout<<"在三根柱子上移"<<n<<"只盘子的步骤为"<<endl;
move(n,a,b,c);
}
void move(int m,char p,char q,char r)
{
p='A';
q='B';
r='C';
if (m==1)
{
cout<<"["<<step<<"]move 1# from"<<" "<<p<<" "<<"to"<<" "<<r<<" "<<endl;
step++;
}
else
{
move(m-1,p,r,q);
cout<<"["<<step<<"]move"<<m<<"# from"<<" "<<p<<" "<<"to"<<" "<<r<<" "<<endl;
step++;
move(m-1,q,p,r);
}
}