void find(int n, int* t)
{
int stack[100];
char res[100];
int r = 0;
int sn = 0;
int i,j;
int m = 0;
for (i=0; i < n && m <= n;m++)
{
int f = 0;
if (m+1 == t[i])
{
res[r++] = 'S';
printf("go directly:%d\n", m+1);
i++;
f = 1;
}
while (sn > 0)
{
if (stack[0] == t[i])
{
f = 1;
printf("pop from stack:%d\n", t[i]);
sn --;
res[r++] = 'O';
for (j = 0; j < sn; j++) stack[j] = stack[j+1];
i++;
continue;
}
else break;
}
if (i >= n) break;
if (f == 0)
{
res[r++] = 'I';
printf("push to stack:%d\n", m+1);
stack[sn++] = m+1;
}
}
if (sn == 0)
{
res[r] = 0;
printf("%s\n", res);
}
else printf("Impossible\n");
}
int main()
{
int d[] = {1,2,3,5,4,6,7};
find(7,d);
//getch();
return 0;
}
看你的程序输入输出范例感觉不是栈,而是队列,你确定你这个是入栈出栈?不是入队列出队列?
给你一个建议,对于任何一个已经EXIT的车厢,其后面都不能有比他小的两节车厢倒序EXIT