队列

来源:互联网 发布:劲酒和椰岛鹿龟酒 知乎 编辑:程序博客网 时间:2024/06/05 09:04

queue队列容器是一个先入先出的线性存储表,元素的插入只能在队尾,元素的删除只能在队首。

queue队列具有入队push(),出队pop(),读取队首元素front(),读取队尾元素back(),判断队列是否为空empty()和队列当前元素的数目size()这几种方法。


#include<queue>
#include<iostream>
using namespace std;
int main()
{
    queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(8);
    cout<<q.size()<<endl;
    cout<<q.front()<<endl;
    cout<<q.back()<<endl;
     cout<<q.empty()<<endl;
    while(q.empty()!=true)
    {
        cout<<q.front()<<" ";
        q.pop();
    }
    cout<<endl;
    return 0;
}


#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    string s;
    int n,m,d;
    queue<string>v;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>s;
            v.push(s);
        }
        cin>>m;
        getchar();
        for(int i=0;i<m-1;i++)
        {
            v.push(v.front());
            v.pop();
        }
        cin>>d;
        int flag;
        while(!v.empty())
        {
            flag=1;
            while(flag!=d)
            {
                v.push(v.front());
                v.pop();
                flag++;
            }




        cout<<v.front()<<endl;
        v.pop();
        }
     }
    return 0;
}




报数Time Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 97(55 users)Total Accepted: 58(50 users)Rating: Special Judge: NoDescription

有N个人围成一圈,按顺时针给他们编号为1-N。

紧接着,指定编号为M的人开始报数,报数按顺时针进行。

报到D的人出列,下一个人重新开始报数。按此规律,每次报到D的人都出列。

要求同学编程求出出列的顺序。

Input

输入包括多组测试用例。

对于每组用例,第一行是一个整数N,表示人数。N<100。

接下来N行是每个人的人名。人名为长度不超过20连续字符串。

最后是以两个以","分割的整数M,D。代表从M个人开始,每报D个数出列。

Output输出所求的顺序Sample Input8
Zhao
Qian
Sun
Li
Zhou
Wu
Zheng
Wang
4,4Sample OutputZheng
Sun
Wang
Zhou
Li
Wu
Qian
ZhaoHint队列模拟Author卢俊达

#include<iostream>#include<queue>#include<stdio.h>#include<string.h>using namespace std;int main(){    string s;    int n,m,d;    queue<string>v;    while(cin>>n)    {        for(int i=0;i<n;i++)        {            cin>>s;            v.push(s);        }        cin>>m;        getchar();        for(int i=0;i<m-1;i++)   //把前m-1个数据放到队尾        {            v.push(v.front());               v.pop();        }        cin>>d;        int flag;        while(!v.empty())            {            flag=1;         //从第m个元素开始flag++            while(flag!=d)  //flag=d跳出循环并输出 出队  下一个元素被标记为1,并成为队首元素            {                v.push(v.front());//把从队首到第d-1个元素放到队尾                v.pop();                flag++;            }        cout<<v.front()<<endl;        v.pop();        }     }    return 0;}


原创粉丝点击