队列的简单讲解

队列queue的特征:先进先出

C++队列queue类成员函数如下:

back()返回最后一个元素

empty()如果队列空则返回真

front()返回第一个元素

pop()删除第一个元素

push()在末尾加入一个元素

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

下面举例讲解:          题目  点击打开链接

  #include <stdio.h>
      #include <iostream>
#include <stdlib.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
struct sj
{
    char a[50];
    int b,c;
};
int main()
{
    queue<int>a;    //定义队列
    queue<int>n;
    queue<int>o;
    sj m[1000];
    int b,c,d,e,f,g,h;
    while(1)
    {
        cin>>b>>c>>d;
        if(d==0&&b==0&&c==0)
            break;
        while(!a.empty())    //队列的初始化
            a.pop();
        while(!n.empty())
            n.pop();
        while(!o.empty())
            o.pop();
        e=0;
        while(1)
        {
            cin>>m[e].a;
            if(strcmp(m[e].a,"#")==0)
                break;
            cin>>m[e].b;
            m[e].c=((m[e].a[0]-'0')*10+(m[e].a[1]-'0'))*60+(m[e].a[3]-'0')*10+(m[e].a[4]-'0');
            e++;
        }
        g=0;
        for(int i=0;i<e;i++)
        {
            if(m[i].b<=2)
            {
                if(b>0)
                {
                    a.push(m[i].c+30);    //入队
                    b--;
                    g=g+m[i].b;
                }
                else
                {
                    if(m[i].c+30>=a.front())
                    {
                        h=a.front();
                        a.pop();
                        if(m[i].c<h)
                            a.push(h+30);
                        else
                            a.push(m[i].c+30);
                        g=g+m[i].b;
                    }
                }
            }
            else if(m[i].b<=4)
            {
                if(c>0)
                {
                    n.push(m[i].c+30);
                    c--;
                    g=g+m[i].b;
                }
                else
                {
                    if(m[i].c+30>=n.front())
                    {
                        h=n.front();
                        n.pop();
                        if(m[i].c<h)
                            n.push(h+30);
                        else
                            n.push(m[i].c+30);
                        g=g+m[i].b;
                    }
                }
            }
            else if(m[i].b<=6)
            {
                if(d>0)
                {
                    o.push(m[i].c+30);
                    d--;
                    g=g+m[i].b;
                }
                else
                {
                    if(m[i].c+30>=o.front())
                    {
                        h=o.front();
                        o.pop();
                        if(m[i].c<h)
                            o.push(h+30);
                        else
                            o.push(m[i].c+30);
                        g=g+m[i].b;
                    }
                }
            }
        }
        cout<<g<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/soul778888/article/details/79758468
今日推荐