坑爹啊,中间输出没去掉,检查了好久……
略恶心的一道题- -
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int st[30][30],a,b,n,top[30],i,j;
char s1[10],s2[10];
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
st[i][0]=i;
top[i]=1;
}
while(scanf("%s",s1)&&strcmp(s1,"quit"))
{
scanf("%d%s%d",&a,s2,&b);
if(strcmp(s1,"move")==0)
{
if(strcmp(s2,"onto")==0)
{
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==a)
break;
if(j!=top[i])
break;
}
int x=i,y=j;
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==b)
break;
if(j!=top[i])
break;
}
if(x==i)
continue;
int y1=j;
for(j=y+1;j<top[x];j++)
{
st[st[x][j]][0]=st[x][j];
top[st[x][j]]=1;
}
top[x]=y;
for(j=y1+1;j<top[i];j++)
{
st[st[i][j]][0]=st[i][j];
top[st[i][j]]=1;
}
st[i][y1+1]=a;
top[i]=y1+2;
}
else if(strcmp(s2,"over")==0)
{
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==a)
break;
if(j!=top[i])
break;
}
int x=i,y=j;
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==b)
break;
if(j!=top[i])
break;
}
if(x==i)
continue;
for(j=y+1;j<top[x];j++)
{
st[st[x][j]][0]=st[x][j];
top[st[x][j]]=1;
}
top[x]=y;
st[i][top[i]]=a;
top[i]++;
}
}
else if(strcmp(s1,"pile")==0)
{
if(strcmp(s2,"onto")==0)
{
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==b)
break;
if(j!=top[i])
break;
}
int x=i,y=j;
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==a)
break;
if(j!=top[i])
break;
}
int y1=j;
if(x==i)
continue;
for(j=y+1;j<top[x];j++)
{
st[st[x][j]][0]=st[x][j];
top[st[x][j]]=1;
}
top[x]=y+1;
for(j=y1;j<top[i];j++)
st[x][top[x]++]=st[i][j];
top[i]=y1;
}
else if(strcmp(s2,"over")==0)
{
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==b)
break;
if(j!=top[i])
break;
}
int x=i;
for(i=0;i<n;i++)
{
for(j=0;j<top[i];j++)
if(st[i][j]==a)
break;
if(j!=top[i])
break;
}
int y=j;
if(x==i)
continue;
for(;j<top[i];j++)
st[x][top[x]++]=st[i][j];
top[i]=y;
}
}
}
for(i=0;i<n;i++)
{
printf("%d:",i);
for(j=0;j<top[i];j++)
printf(" %d",st[i][j]);
printf("\n");
}
}
return 0;
}