栈 铁轨 火车编组NEFU1628

在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;
stack<int>vis1;
int main()
{
    int n;
    while(cin>>n)
    {
        int s[110];
        int a=1,b=1;
        for(int i=1; i<=n; i++)
            cin>>s[i];
        while(b<=n)//从1开始输入,每次有三种情况
        {
            if(a==s[b])//这个数正好是要输出的就进栈后直接输出
            {
                a++;
                b++;
                cout<<'A'<<'B';

            }
            else if(!vis1.empty()&&vis1.top()==s[b])//如果栈非空且栈顶元素正好是要输出的,就直接出栈
            {
                vis1.pop();
                b++;
                cout<<'B';
            }
            else if(a<=n)//如果不是输出的先放入栈中缓存
            {
                vis1.push(a++);
                cout<<'A';
            }


        }

    }
    return 0;
}

发布了11 篇原创文章 · 获赞 2 · 访问量 333

猜你喜欢

转载自blog.csdn.net/qq_45687002/article/details/104216180