判断——出栈能否实现

#include<cstdio>
#include<iostream>
#include<stack>
 using namespace std;
 
int main()
{
     int n,i,k;
     int a[1010];
     stack<int> s;
     while(scanf("%d",&n),n)
     {
       while(scanf("%d",&a[1]))

         {
             if(a[1]==0)

             {

                 printf("\n");
                 break;
             }
             while(!s.empty())       //初始化清空栈
                 s.pop();

             k=1;
             for(i=2;i<=n;++i)
                 scanf("%d",&a[i]);
            for(i=1;i<=n;++i)
             {
                 s.push(i);
                 if(i==a[k])
                 {
                    while(!s.empty()&&s.top()==a[k])
                     {
                         s.pop();
                         ++k;
                     }
                 }
             }
             if(s.empty())
                 printf("Yes\n");
             else
                 printf("No\n");
         }
     }
     return 0;
}

posted @ 2015-04-18 16:21  Spiderman.L  阅读(117)  评论(0编辑  收藏  举报