1 条题解

  • 0
    @ 2022-11-2 14:44:25

    手动实现循环队列

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int op, x;
    
    const int MAX_SIZE = 100;
    int q[MAX_SIZE], head, tail, sz;
    
    int main()
    {
        cin >> n;
    
        head = 0, tail = -1, sz = 0;
    
        for (int i = 1; i <= n; i++)
        {
            cin >> op >> x;
            if (op == 1)
            {
                tail = (tail + 1) % MAX_SIZE;
                q[tail] = x;
                sz++;
            }
            else if (op == 2)
            {
                if (sz > 0)
                    cout << q[head] << "\n";
                else
                    cout << -1 << "\n";
            }
            else if (op == 3)
            {
                if (sz > 0)
                {
                    head++;
                    sz--;
                }
            }
        }
        return 0;
    }
    

    STL 实现

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int op, x;
    queue<int> q;
    
    int main()
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> op >> x;
            if (op == 1)
            {
                q.push(x);
            }
            else if (op == 2)
            {
                if (q.size() > 0)
                    cout << q.front() << "\n";
                else
                    cout << -1 << "\n";
            }
            else if (op == 3)
            {
                if (!q.empty())
                    q.pop();
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1157
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    70
    已通过
    44
    上传者