2021团体程序设计天梯赛 L2-1 包装机

思路:

栈和队列,轨道为队列筐为栈,按下筐按钮从筐中拿一个物品;按下轨道按钮,从轨道拿一个物品放到筐。若筐已满,则先从筐中拿一个物品;若轨道或筐为空,则按对应按钮不会发生任何事。

Tip:

注意语言顺序即可

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1000 + 5;
queue<char> que[maxn];
stack<char> s;

int main() {
    int n, m, smax;
    cin >> n >> m >> smax;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            char c;
            cin >> c;
            que[i].push(c);
        }
    int nop;
    while (cin >> nop) {
        if (nop == -1)
            break;
        if (nop == 0) {
            if (!s.empty()) {
                cout << s.top();
                s.pop();
            }
        } else {
            if (que[nop].empty())
                continue;
            if (s.size() == smax) {
                cout << s.top();
                s.pop();
            }
            s.push(que[nop].front());
            que[nop].pop();
        }
    }
    return 0;
}

  

posted @ 2021-04-27 10:14  Whiteying  阅读(331)  评论(0编辑  收藏  举报