蓝桥杯 ADV-233 算法提高 队列操作

栏目: 编程工具 · 发布时间: 5年前

内容简介:问题描述队列操作题。根据输入的操作命令,操作队列(1)入队、(2)出队并输出、(3)计算队中元素个数并输出。输入格式

问题描述

队列操作题。根据输入的操作命令,操作队列(1)入队、(2)出队并输出、(3)计算队中元素个数并输出。

输入格式

第一行一个数字N。

下面N行,每行第一个数字为操作命令(1)入队、(2)出队并输出、(3)计算队中元素个数并输出。

输出格式

若干行每行显示一个2或3命令的输出结果。注意:2.出队命令可能会出现空队出队(下溢),请输出“no”,并退出。

样例输入

7

1 19

1 56

2

3

2

3

2

样例输出

19

1

56

0

no

数据规模和约定

1<=N<=50

分析:用C++的STL队列实现~

#include <iostream>
#include <queue>
using namespace std;
int main() {
    int n;
    cin >> n;
    queue<int> q;
    for (int i = 0; i < n; i++) {
        int query, temp;
        cin >> query;
        if (query == 1) {
            cin >> temp;
            q.push(temp);
        } else if (query == 2) {
            if (q.empty()) {
                cout << "no" << endl;
                return 0;
            } else {
                cout << q.front() << endl;
                q.pop();
            }
        } else if (query == 3) {
            cout << q.size() << endl;
        }
    }
    return 0;
}
❤❤点击这里 -> 订阅PAT、GPLT天梯赛、LeetCode题解离线版❤❤ 蓝桥杯 ADV-233 算法提高 队列操作

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Linux Programming Interface

The Linux Programming Interface

Michael Kerrisk / No Starch Press / 2010-11-6 / GBP 79.99

The Linux Programming Interface describes the Linux API (application programming interface)-the system calls, library functions, and other low-level interfaces that are used, directly or indirectly, b......一起来看看 《The Linux Programming Interface》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试