看病排队 - 升级版

文章介绍了如何使用优先队列和vector数据结构解决医院挂号系统中,根据优先级和类型码确定病人就诊顺序的问题。两种方法分别展示了如何处理新来病人(IN)和看完病病人(OUT)的操作。
摘要由CSDN通过智能技术生成

目录

描述

输入

输出

样例输入

样例输出

思路

code(优先队列)

code(vector)


描述

医院中病人需排队挂号看病;有三类号普通号,军人优先号,老人优先号,急诊优先号;

优先级为:急诊优先号 类型为1;>老人优先号,类型为2>军人优先号,类型为3>普通号类型为4;同一类型的号按序看病;

病人挂号的码为:类型码 序号;

病人来了用IN, 病人走了用OUT;

现病人来的顺序如下,请写出病人看好病离开的顺序。

输入

来了的病人挂号的码为:IN 类型码 序号;

看好了用OUT;

输出

看好病的号码

样例输入
IN 4 1
OUT
IN 1 3
IN 2 1
IN 4 2
IN 3 2
OUT
IN 1 1
OUT
IN 2 2
IN 4 3
IN 3 1
OUT
OUT
OUT
OUT
IN 4 4
OUT
OUT
OUT
样例输出
4 1
1 3
1 1
2 1
2 2
3 1
3 2
4 2
4 3
4 4

思路

下面将用2中不同的数据结构去解决次问题 第1中用优先队列,第2中用vector 区别:优先队列的方法要注意运算符的重载,vector的方法在out操作时要对他进行排序

code(优先队列)

#include<iostream>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
struct node{
    int priority,id;
};
bool operator < (const node &a,const node &b){
    if(a.priority == b.priority) return a.id>b.id;//id小的优先级高 
    return a.priority  > b.priority;//类型码小的优先级高 
}
int main()
{
    int n;
    priority_queue <node> q;
    string cz;
    while(cin>>cz){
        if(cz=="IN"){
            node patient;
            cin>>patient.priority>>patient.id;
            //注意在加入队列中要用node patient 
            q.push(patient);
        }
        else{
            node patient = q.top();
            q.pop();
            cout<<patient.priority<<" "<<patient.id<<endl;
        }
    }
    return 0;
}

code(vector)

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
struct node{
    int priority,id;
};
bool cmp(const node& a,const node& b){
    if(a.priority==b.priority) return a.id < b.id;
    return a.priority < b.priority;//急诊优先号小的放前面 
}
int main()
{
    int n;
    vector <node> vc;
    string cz;
    while(cin>>cz){
        if(cz=="IN"){
            node patient;
            cin>>patient.priority>>patient.id;
            //注意在加入队列中要用node patient 
            vc.push_back(patient);
        }
        else{
            sort(vc.begin(),vc.end(),cmp);//排序  
            node patient = vc.front();
            vc.erase(vc.begin());//病人看好病离开 
            cout<<patient.priority<<" "<<patient.id<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值