2 条题解

  • 2
    @ 2024-2-11 19:33:50

    来发模拟

    #include <bits/stdc++.h>
    using namespace std;
    int n, a[105];
    stack<int> s;
    int main()
    {
        ios::sync_with_stdio(false);  // 读入加速
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        // 输入
        int cnt = 1, l = 2;  // cnt代表数组a中第cnt个元素,l代表的是依次入栈的元素
        s.push(-1);  // 之所以要先压入-1,是因为如果第一个元素压入后立马就弹出了,这样栈就为空,所以就会出现RE的情况,这里是防止栈为空(注:作者就是因为这个问题修了半天)
        s.push(1);  // 压入第一个元素
        cout << "A";  // 输出压入一个元素
        while (cnt <= n)  // 当cnt小于n是在执行
        {
            if (s.top() == a[cnt])  // 如果栈顶等于当前需要弹出的元素
            {
                cout << "B";  // 输出B,代表弹出
                cnt++;  // 将cnt指向下一个元素
                s.pop();  // 将此时需弹出的栈顶元素弹出
            }
            else
            {
                cout << "A";  // 否则输出压入
                s.push(l);  // 压入
                l++;  // 将需压入的元素加1
            }
        }
        return 0;  // 好习惯
    }
    
    • 1
      @ 2023-3-28 7:49:24
      #include <bits/stdc++.h> 
      using namespace std;
      stack<int> st; //stl栈;
      int n, in = 1;
      int main()
      {
          cin >> n;
          while (n--)
          {
              int x;
              cin >> x;
              if (in > x)
                  st.pop();
              else
                  while (in<=x)
                  {
                      st.push(in);
                      putchar('A');
                      in++;
                  }
                  putchar('B');
          }
          return 0;
      }
      
      • 1

      信息

      ID
      787
      时间
      1000ms
      内存
      128MiB
      难度
      3
      标签
      递交数
      47
      已通过
      27
      上传者