資源簡介
任意輸入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(“請輸入盤子數:\n“);
scanf(“%d“&n);
init(tn);
printf(“原始的數據為:\n“);
showdata(t);
move(tn);
printf(“移動后數據為:\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題論文
- 下一篇:財政與金融形考作業一.docx
評論
共有 條評論