队列和栈

来源:互联网 发布:重庆软件行业公司 编辑:程序博客网 时间:2024/05/16 15:53

#include<cstdio>#include<stack>using namespace std;const int MAXN=1010;int n,target[MAXN];int main(){    while(~scanf("%d",&n))    {        stack<int> 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++;            }            else if(A<=n)                s.push(A++);            else            {                ok=0;                break;            }        }        printf("%s\n",ok?"Yes":"No");    }    return 0;}<span style="color:#cc0000;"></span>


队列

#include<cstdio>#include<queue>using namespace std;int main(){    int n;    queue<int> q;    scanf("%d",&n);    for(int i=1;i<=n;i++)        q.push(i);    while(!q.empty())    {        printf("%d ",q.front());        q.pop();        q.push(q.front());        q.pop();    }    return 0;}



0 0
原创粉丝点击