某城市有一个火车站,有n节车厢从A方向驶入车站,编序号为1~n。你的任务时判断他是否能以一种特定方式进入B方向的车站。
#include<cstdio>
#include<stack>
using namespace std;
const int MAXN=1000+10;
int n,target[MAXN];
int main()
{
while(scanf("%d", &n) == 1){
stack<int> s;//声明了一个栈,这个栈的名字是s
int A = 1, B = 1;
for(int i = 1; i <= n; i++)
scanf("%d", &target[i]);
int ok = 1;
while(B <= n){
if(A == target[B]){ A++; B++; }
else if(!s.empty() && s.top() == target[B]){ s.pop(); B++; }
//关于此栈的头部表示为s.top() s.pop()出栈的问题
else if(A <= n) s.push(A++);//入栈的问题
else { ok = 0; break; }
}
printf("%s\n", ok ? "Yes" : "No");
}
return 0;
}
主要是关于栈的知识,关于栈的建立,栈的出栈入栈的函数表示。
181

被折叠的 条评论
为什么被折叠?



