資源簡(jiǎn)介
任意輸入N個(gè)盤(pán),在三個(gè)柱子上實(shí)現(xiàn)漢諾塔問(wèn)題的非遞歸求解,用棧進(jìn)行
代碼片段和文件信息
#include?
#include
#include
#include
#define?H?3
typedef?struct
{
int?a[100];
int?top;
char?name;
}ST;
void?init(ST?*tint?n);
void?move(ST?*tint?n);
void?showdata(ST?*t);
void?push(ST?*tint?index);
int?pop(ST?*t);
int?gettop(ST?*t);
long?int?expo(int?xint?y);
main()
{
ST?t[3];
int?n;
printf(“請(qǐng)輸入盤(pán)子數(shù):\n“);
scanf(“%d“&n);
init(tn);
printf(“原始的數(shù)據(jù)為:\n“);
showdata(t);
move(tn);
printf(“移動(dòng)后數(shù)據(jù)為:\n“);
showdata(t);
}
void?init(ST?*tint?n)
{
int?i;
t[0].top=0;
t[0].name=‘A‘;
for(i=n;i>=1;i--)
{
push(&t[0]i);
}
???if(H%2==0)
???{
???t[1].name=‘B‘;
???t[1].top=0;
???t[2].name=‘C‘;
???t[2].top=0;
???}
???else
???{
???t[1].name=‘C‘;
???t[1].top=0;
???t[2].name=‘B‘;
???t[2].top=0;
???}
}
void?push(ST?*tint?index)
{
if(t->top>=0)
{
t->top++;
t->a[t->top]=index;
}
else
printf(“wrong!\n“);
}
int?pop(ST?*t)
{
int?x;
if(t->top!=0)
{
x=t->a[t->top];
t->top--;
return?x;
}
else?exit(0);
}
void?showdata(ST?*t)
{
int?ij;
for(j=0;j {
if(t[j].top!=0)
{
?????printf(“zhu
- 上一篇:2019年美賽D題論文
- 下一篇:財(cái)政與金融形考作業(yè)一.docx
評(píng)論
共有 條評(píng)論