码迷,mamicode.com
首页 > 其他好文 > 详细

判断——出栈能否实现

时间:2015-04-18 17:36:34      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

判断——出栈能否实现

标签:

原文地址:http://www.cnblogs.com/aimqqroad-13/p/4437602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!