首页 > 代码库 > 栈-----火车的模拟

栈-----火车的模拟

 1 //栈的使用   火车的模拟  2  3 #include<cstdio> 4 #include<iostream> 5 #include<stack> 6  7 using namespace std; 8  9 const int MAXN = 1000 + 10;10 11 12 int n,target[MAXN];13 14 int main(){15     while(scanf("%d", &n)==1){16         stack<int> s;17     int A = 1 , B = 1;18     for(int i = 1; i <= n; i++)19        scanf("%d",&target[i]);20     int ok = 1;21     while(B <= n){22         if(A == target[B]){ A++; B++;}23         else if(!s.empty() && s.top() == target[B]){ s.pop(); B++;}24         else if(A <= n)s.push(A++);25         else{ ok = 0; break;}26     }27     printf("%s\n",ok?"Yes":"No");28     }29     30     return 0;31 }

 

栈-----火车的模拟