看看能否將結果,從 5、4、3、2、1,倒過來排回原本的順序
#include <stdio.h>
#define MAX 1000
int top = -1, stack[MAX];
void push(int);
int pop();
int main() {
int n, i, res[MAX], t;
while (1) {
scanf("%d", &n); if (n <= 0) break;
while (1) {
scanf("%d", &res[0]); if (res[0] == 0) break;
for (i = 1; i < n; i++) {
scanf("%d", &res[i]);
}
i = n - 1; t = n; push(res[i]);
while (1) {
if (top >= 0 && stack[top] == t) {
pop();
if(t==1) break;
t--;
}
else if (i > 0) {
i--;
push(res[i]);
}
else {
break;
}
}
if(top == -1) printf("Yes");
else printf("No");
puts("");
for (i = top; i >= 0; i--) {
pop();
}
}
}
return 0;
}
void push(int data) {
if (top == MAX - 1)
printf("The stack is full.\n");
else {
top++;
stack[top] = data;
}
}
int pop() {
int data;
if (top == -1) {
printf("The stack is empty.\n");
return -1;
}
else {
data = stack[top];
stack[top] = 0;
top--;
return data;
}
}