标签:
利用C++自带的栈结构
源代码如下:
#include<cstdio> #include<stack> using namespace std; const int MAX =1000 +10; int target[MAX]; int n; int main() { while(scanf("%d",&n)==1) { stack<int> s; int temp=1; int i=1,A=1,B=1; for(i=1;i<=n;i++) scanf("%d",&target[i]); while(B<=n) { if(A==target[B]) { A++;B++; } else if(!s.empty() && s.top()==target[B]) { s.pop(); B++; } else if(A<=n) s.push(A++); else { temp=0; break; } } printf("%s\n", temp? "yes" :"no"); } }
实验截图如下:
标签:
原文地址:http://www.cnblogs.com/ly-rabbit-wust/p/5575163.html