队列的应用

报数

题目:有 n 个小朋友做游戏,他们的编号分别是1,2,3…n。他们按照编号从小到大依次顺时针围成一个圆圈,从第一个小朋友开始从1 报数,依次按照顺时针方向报数(加一),报 m 的人会离开队伍,然后下一个小朋友会继续从 1 开始报数,直到只剩一个小朋友为止。
输入格式
第一行输入俩个整数,n,m。(1≤n,m≤1000)
输出格式
输出最后一个小朋友的编号,占一行。
样例输入
10 3
样例输出
3

#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
queue<int>q;

void fun(int n,int m)
{
    int cnt=0;
    while(q.size()>1){
        cnt++;
        if(cnt==m){//如果报数报到了m,就直接弹出队列
            q.pop();
            cnt=0;
        }
        else{//否则,取出队列最前端元素放在末尾
            int temp=q.front();
            q.pop();
            q.push(temp);
        }
    }
    cout<<q.front();
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        q.push(i);
    }
    fun(n,m);
    return 0;
}

 

敲七

题目:有一种酒桌游戏叫做“敲7”,规则是从一个人开始,说出任意数字,其他人会顺序往后报,如果一个数字包含 7,或者是 7 的倍数,那么需要敲打杯子或盘子,不能说出。
现在 n 个人围坐在一个圆桌周围,他们编号从 1 到n 顺时针排列。从某一人开始报出一个数字,其他人会按照顺时针方向顺序往后报(加一),如果某个人的数字包含 7,或者是 7 的倍数,那么他将退出游戏,下一个人继续接着报,直到剩一个人为止。
输入格式
第一行输入三个整数,n,m,t。n 代表总人数,m 代表从第 m 个人开始报数,他报出的数字是 t。1≤m≤n≤1000,1≤t≤100)接下来的 n 行,每一行输入一个字符串,代表这 n 个人的名字,字符串的长度不超过 20。
输出格式
输出剩下的那个人的名字,占一行。
样例输入
5 3 20
donglali
nanlali
xilali
beilali
chuanpu
样例输出
chuanpu
代码:

#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
queue<string>q;
void  fun(int n,int t)
{
    for(int j=t;q.size()>1;j++){
        if (j % 7 == 0 //9999
                || (j>10 && j<100 && (j / 10 == 7 || j % 10 == 7))
                || (j>100 && j<1000 && (j / 100 == 7 || j % 10 == 7 || j / 10 % 10 == 7))
                || (j>1000 && j<10000 && (j / 1000 == 7 || j % 10 == 7 || j / 100 % 10 == 7 || j % 100 / 10 == 7)))
                q.pop();
        else{
            string temp=q.front();
            q.pop();
            q.push(temp);
        }
    }
    cout<<q.front()<<endl;
}
int main()
{
    int n,m,t;
    cin>>n>>m>>t;
    while(n--){
        string s;
        cin>>s;
        q.push(s);
    }
    for(int i=1;i<=m;i++){
        if(i<m){
            string temp=q.front();
            q.pop();
            q.push(temp);
        }
    }
    fun(n,t);
    return 0;
}

 

任务系统

题目:蒜头君设计了一个任务系统。这个系统是为了定时提醒蒜头君去完成一些事情。

系统大致如下,初始的时候,蒜头君可能会注册很多任务,每一个任务的注册如下:

Register Q_num Period
表示从系统启动开始,每过 Period 秒提醒蒜头君完成编号为 Qnum的任务。

你能计算出蒜头君最先被提醒的 k 个任务吗?

样例输入

2 5
Register 2004 200
Register 2005 300

样例输出

2004
2005
2004
2004
2005

代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
map<int,int>m;
struct task{
    int num,time;
    bool operator<(const task &A)const{//用于控制优先队列弹出元素的先后顺序
        if(time==A.time) return num>A.num;
        return time>A.time;
    }
};
priority_queue<task>q;
int main()
{
    int n,k;
    cin>>n>>k;
    string s;
    task ts;
    while(n--){
        cin>>s;
        cin>>ts.num>>ts.time;
        q.push(ts);
        m[ts.num]=ts.time;
    }
    while(k--){
        task cur=q.top();
        q.pop();
        cur.time+=m[cur.num];
        q.push(cur);
        cout<<cur.num<<endl;
    }
    return 0;
}
posted @ 2019-04-27 11:24  里昂静  阅读(283)  评论(0编辑  收藏  举报