全部评论 3

  • #include <iostream>
    #include <list>
    #include <unordered_set>
    using namespace std;

    int main() {
    int M, N;
    // 输入展示架容量M和礼物数量N
    cin >> M >> N;

    list<int> shelf;          // 存储展示架上的礼物,按放入顺序排列
    unordered_set<int> exist; // 快速判断礼物是否已存在
    int happy = 0;            // 高兴程度
    
    for (int i = 0; i < N; ++i) {
        int gift;
        cin >> gift;
        
        // 如果礼物已存在,跳过
        if (exist.find(gift) != exist.end()) {
            continue;
        }
        
        // 礼物不存在,高兴度+1
        happy++;
        
        // 如果展示架已满,移除最早放入的礼物
        if (shelf.size() == M) {
            int oldest = shelf.front(); // 取最早放入的礼物
            shelf.pop_front();          // 从展示架移除
            exist.erase(oldest);        // 从集合中删除
        }
        
        // 放入新礼物
        shelf.push_back(gift);
        exist.insert(gift);
    }
    
    // 输出最终高兴程度
    cout << happy << endl;
    
    return 0;
    

    }

    2026-02-04 来自 广东

    0
  • #include <iostream>
    #include <queue>
    using namespace std;

    int main() {
    int n;
    cin >> n; // 读取操作次数
    queue<int> q; // 定义存储整数的队列

    for (int i = 0; i < n; ++i) {
        int op;
        cin >> op; // 读取操作类型(1/2/3)
    
        if (op == 1) {
            // 入队操作:读取要入队的数值并加入队列
            int num;
            cin >> num;
            q.push(num);
        } else if (op == 2) {
            // 出队操作:先判断队列是否为空
            if (q.empty()) {
                cout << "impossible!" << endl;
            } else {
                q.pop(); // 合法则弹出队首
            }
        } else if (op == 3) {
            // 访问队首:先判断队列是否为空
            if (q.empty()) {
                cout << "impossible!" << endl;
            } else {
                cout << q.front() << endl; // 输出队首元素
            }
        }
    }
    
    return 0;
    

    }

    2026-02-04 来自 广东

    0
  • #include <iostream>
    #include <queue>
    #include <string>
    using namespace std;

    int main() {
    queue<string> q; // 存储客户号码的队列
    string input; // 存储每行输入

    while (true) {
        getline(cin, input); // 读取整行输入(处理带空格的out指令)
        
        // 1. 判断是否是end指令
        if (input == "end") {
            // 处理end:输出剩余所有号码,无则不输出
            if (!q.empty()) {
                while (!q.empty()) {
                    cout << q.front() << " ";
                    q.pop();
                }
                cout << endl;
            }
            break; // 结束程序
        }
    
        // 2. 判断是否是out指令(以"out "开头)
        else if (input.substr(0, 4) == "out ") {
            int k = stoi(input.substr(4)); // 提取叫号数量k
            int count = 0;                 // 记录实际叫号的数量
            bool hasOutput = false;        // 标记是否有输出内容
    
            // 取出前k个(或剩余所有)号码并输出
            while (count < k && !q.empty()) {
                if (hasOutput) {
                    cout << " "; // 元素间加空格,开头不加
                }
                cout << q.front();
                q.pop();
                count++;
                hasOutput = true;
            }
    
            // 输出结果:有内容则换行,无则输出empty
            if (hasOutput) {
                cout << endl;
            } else {
                cout << "empty" << endl;
            }
        }
    
        // 3. 否则是客户号码(7位以1开头的数字)
        else {
            // 简单验证号码格式(可选,题目保证输入合法可省略)
            if (input.size() == 7 && input[0] == '1') {
                q.push(input); // 号码入队
            }
        }
    }
    
    return 0;
    

    }

    2026-02-04 来自 广东

    0

热门讨论