08.STL队列

news/2024/7/7 20:47:26

1.基本操作

#include<iostream>
#include <stack>
#include<queue>
using  namespace std;
queue<int> q;
stack<int> stk;
void text01(){
    stk.push(1);stk.push(2);stk.push(3);stk.push(4);
    q.push(5);q.push(6);q.push(7);q.push(8);
    cout<<"栈输出:"<<endl;
    while(!stk.empty()){//输出一个删一个
        cout<<stk.top()<<" ";
        stk.pop();
    }
    cout<<endl;
    while(!q.empty()){//输出一个删一个
        cout<<q.front()<<" ";
        q.pop();
    }
}
int main() {
    text01();
    return 0;
}

2.应用

模拟环的方式都右哪些?

线性扩增2倍模拟环

数组的循环位移

线性队列模拟环

因为队列是头删尾插,通过把pop的数据用临时变量接收,然后再立马push,就可以达到环的效果

3.习题

1332:【例2-1】周末舞会

#include<iostream>
#include <stack>
#include<queue>
using  namespace std;
queue<int> man,woman;
int n1,n2,song;
void text01(){
    cin>>n1>>n2>>song;
    for(int i=1;i<=n1;i++){
        man.push(i);
    }
    for(int i=1;i<=n2;i++){
        woman.push(i);
    }
    for(int i=1;i<=song;i++){
        int x1=man.front();
        int x2=woman.front();
        man.pop();woman.pop();
        cout<<x1<<" "<<x2<<endl;
        man.push(x1);woman.push(x2);
    }
}
int main() {
    text01();
    return 0;
}

1334:【例2-3】围圈报数

#include<iostream>
#include <stack>
#include<queue>
using  namespace std;
queue<int> q;
int n,m,k=1;
void text01(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        q.push(i);
    }
    while(q.size()!=1){
        if(k!=m){
            int x=q.front();
            q.pop();
            q.push(x);
            k++;
        }
        else{
            k=1;
            cout<<q.front()<<" ";
            q.pop();
        }
    }
    cout<<q.front();
}
int main() {
    text01();
    return 0;
}

P1540 [NOIP2010 提高组] 机器翻译

#include<iostream>
#include <stack>
#include<queue>
using  namespace std;
queue<int> q;
const int N=1e3+10;
int a[N],m,n,cnt;
bool vis[N];
void text01(){
    cin>>m>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(!vis[a[i]]){
            if(q.size()==m){
                vis[q.front()]=false;
                q.pop();
            }
            q.push(a[i]);
            vis[a[i]]=true;
            cnt++;
        }
    }
    cout<<cnt<<endl;
}
int main() {
    text01();
    return 0;
}

1333:【例2-2】Blah数集

#include<iostream>
#include <stack>
#include<queue>
using  namespace std;
const int N=1e6+10;
int a1,n1;
void Blah_Solution(int a,int n){
    int k=1;
    queue<int> q1,q2;
    q1.push(2*a+1);
    q2.push(3*a+1);
    int x;
    while(k!=n){
        if(q1.front()<q2.front()){
            x=q1.front();
            q1.pop();
        }
        else if(q1.front()==q2.front()){
            x=q1.front();
            q1.pop();
            q2.pop();
        }
        else{
            x=q2.front();
            q2.pop();
        }
        k++;
        q1.push(2*x+1);
        q2.push(3*x+1);
    }
    cout<<x<<endl;
}
void text01(){
    while(cin>>a1>>n1){
        Blah_Solution(a1,n1);
    }
}
int main() {
    text01();
    return 0;
}

http://lihuaxi.xjx100.cn/news/2041675.html

相关文章

基于Spring Boot的安康旅游网站的设计与实现,计算机毕业设计(带源码+论文)

源码获取地址&#xff1a; 码呢-一个专注于技术分享的博客平台一个专注于技术分享的博客平台,大家以共同学习,乐于分享,拥抱开源的价值观进行学习交流http://www.xmbiao.cn/resource-details/1760645517548793858

【leetcode刷题之路】面试经典150题(4)——栈+链表

文章目录 7 栈7.1 【哈希表】有效的括号7.2【栈】简化路径7.3 【栈】最小栈7.4 【栈】逆波兰表达式求值7.5 【栈】基本计算器 8 链表8.1 【双指针】环形链表8.2 【双指针】两数相加8.3 【双指针】合并两个有序链表8.4 【哈希表】随机链表的复制8.5 【链表】反转链表 II8.6 【链…

穿越Redis单线程迷雾:从面试场景到技术内核的解读

目录 ​编辑 前言 Redis中的多线程 I/O多线程 Redis中的多进程 结论 延伸阅读 前言 很多人都遇到过这么一道面试题&#xff1a;Redis是单线程还是多线程&#xff1f;这个问题既简单又复杂。说他简单是因为大多数人都知道Redis是单线程&#xff0c;说复杂是因为这个答案…

五、全局scss变量定义及使用

定义 variable.scss 存放全局变量 // base color $blue:#324157; $light-blue:#3A71A8; $red:#C03639; $pink: #E65D6E; $green: #30B08F; $tiffany: #4AB7BD; $yellow:#FEC171; $panGreen: #30B08F;// 默认菜单主题风格 $base-menu-color:#bfcbd9; $base-menu-color-active:#f…

Linux:Jenkins用户权限和管理

1.下载插件 由于Jenkins的默认权限管理并不是很精细所以我们安装一个插件进行权限的一个管理 插件名称为&#xff1a;Role-based Authorization Strategy 安装完插件我们再去配置一下 进入全局安全配置 选择这个Role-Based Strategy策略然后保存 2.创建角色 我们这里主要使…

dp入门(模板题)

解法一&#xff1a; 双指针&#xff0c;没必要开数组直接边输边算&#xff0c;max至少要2个数&#xff0c;补一个数时的特判 #include <iostream> #include <vector> #include <algorithm> using namespace std; #define endl \nint main() {ios::sync_wit…

C语言自定义类型:结构体的使用及其内存对齐【超详细建议点赞收藏】

目录 1. 结构体类型的声明1.1 结构的声明1.2 结构体变量的创建和初始化1.3 结构的特殊声明---匿名结构体1.4 结构的自引用 2.结构体内存对齐&#xff08;重点&#xff01;&#xff01;&#xff09;2.1 对齐规则2.2 例题讲解2.3 为什么存在内存对齐&#xff1f;2.4 修改默认对齐…

二进制、十进制、八进制、十六进制 各代表的英文字母是什么

二进制、十进制、八进制、十六进制 各代表的英文字母是什么 AllenLeungX 于 2019-07-08 08:42:47 发布 分类专栏&#xff1a; 其它 文章标签&#xff1a; 二进制、十进制、八进制、十六进制 英文和缩写 二进制是Binary&#xff0c;简写为B 八进制是Octal&#xff0c;简写为O 十…