算法笔记-第七章-队列

算法笔记-第七章-队列

队列的相关知识点

大佬的讲解

c++中队列queue用法

一:queue是一种容器转换器模板,调用#include< queue>即可使用队列类

二:使用queue 表示队列

三:相关函数

1.push() 在队尾插入一个元素

2.pop() 删除队列第一个元素

3.size() 返回队列中元素个数

4.empty() 如果队列空则返回true

5.front() 返回队列中的第一个元素

6.back() 返回队列中最后一个元素

队列的操作序列

dart 复制代码
//队列的操作
//简单形式压入和输出
#include <iostream>
#include <string>
#include <queue>
using namespace std;

int main() {
    int n, k;
    cin >> n;

    string action;//这个是输出判断的字符串

    queue<int> q;//定义队列
    for (int i = 0; i < n; i++)
    {
        cin >> action;

        if (action == "push") 
        {
            cin >> k;
            q.push(k);
        }
        else
        {
            if (q.empty()) 
            {
                cout << -1 << endl;   
            }
            else    
            {
                cout << q.front() << endl;   
                q.pop();   
            }
        }
    }
    return 0;   
}

求和队列


dart 复制代码
#include <cstdio>
#include <queue>
using namespace std;

int main() 
{
    int n, x;
    scanf("%d", &n);
    queue<int> q;

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &x);  
        q.push(x);  
    }
    //思路:取出一个数,再取出一个数,然后相加再加入队列中  
    while (q.size() > 1)   
    {
        int front1 = q.front();  
        q.pop();  
        int front2 = q.front();  
        q.pop();  
        q.push(front1 + front2);  
    }
    printf("%d", q.front());  
    return 0;  
}

约瑟夫环-队列


dart 复制代码
//约瑟夫环 - 队列
#include <cstdio>
#include <queue>
using namespace std;

int main() 
{
    int n, k;
    scanf("%d%d", &n, &k);//输入多少个数,匹配到那个位置
    queue<int> q;//队列

    for (int i = 1; i <= n; i++) //先输入数据到队列中
    {
        q.push(i);
    }


    while (!q.empty()) //用一个循环,不断的输出队列里面的数
    {
        for (int i = 0; i < k - 1; i++) //将第k的顺序的数排到队列的第一个位置
        {
            int front = q.front();//输出第一个数   

            q.pop();//删除第一个数操作   

            q.push(front);//在队尾插入第一个数   

        }
        printf("%d", q.front());   
        q.pop();   

        if (!q.empty())    
        {
            printf(" ");   
        }
    }
    return 0;   
}

匹配队列



dart 复制代码
#include <cstdio>
#include <queue>//队列
using namespace std;

int main() 
{
    int n, x;
    scanf("%d", &n);
    queue<int> q1, q2;

    for (int i = 0; i < n; i++) //先搞定队列q1
    {
        scanf("%d", &x);
        q1.push(x);
    }

    for (int i = 0; i < n; i++) //再搞定队列q2
    {
        scanf("%d", &x);
        q2.push(x);
    }


    int counter = 0;//表示需要执行的操作数

    while (!q1.empty()) //这个是以q1为主导的
                        //q2是进行匹配的次要
    {
        if (q1.front() == q2.front()) 
        {
            q1.pop();
            q2.pop();
        }
        else 
        {
            q2.push(q2.front());//匹配不成功那就进行将q2的前面放到后面
            q2.pop();
        }
        counter++;
    }
    printf("%d", counter);//输出匹配的数目
    return 0;
}
相关推荐
研究点啥好呢12 小时前
DJI 机器人视觉算法工程师 面试题精选:10道高频考题+答案解析(背诵版)
算法·面试·机器人·dji
Hello_Embed12 小时前
libmodbus 移植到 STM32H5
笔记·stm32·单片机·嵌入式硬件·嵌入式·ai编程
热心网友俣先生12 小时前
2026年认证杯二阶段A题赛题解析
人工智能·算法·机器学习
Biocloudy12 小时前
循环肿瘤细胞的分离和分型技术
人工智能·经验分享·笔记·其他
Black蜡笔小新12 小时前
自动化AI算法训练服务器DLTM深度学习推理工作站AI赋能质检助力制造业智能化转型
人工智能·算法·自动化
小雅痞12 小时前
[Java][Leetcode simple] 205. 同构字符串
java·算法·leetcode
Undergoer_TW12 小时前
【SLAM性能评估笔记】公开的Vo性能评估工具调研与局限性分析
笔记·evo·kitti·vo·性能评估·tum
空太Jun12 小时前
Git 使用学习笔记
笔记·git·学习
智者知已应修善业13 小时前
【51单片机独立按键控制数码管自增自减】2023-10-5
c++·经验分享·笔记·算法·51单片机
2301_8008951013 小时前
第十四届蓝桥杯国赛b组真题---备战国赛版h
算法·蓝桥杯·深度优先