您的位置:首页 > 产品设计 > UI/UE

HDU 1509 Windows Message Queue

2017-03-09 09:02 471 查看
[align=left]Problem Description[/align]
Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue.
Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting
messages to and getting message from the message queue.
 

[align=left]Input[/align]
There's only one test case in the input. Each line is a command, "GET" or "PUT", which means getting message or putting message. If the command is "PUT", there're one string means the message name and two integer means the parameter
and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.
 

[align=left]Output[/align]
For each "GET" command, output the command getting from the message queue with the name and parameter in one line. If there's no message in the queue, output "EMPTY QUEUE!". There's no output for "PUT" command.
 

[align=left]Sample Input[/align]

GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET

 

[align=left]Sample Output[/align]

EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!

 

[align=left]Author[/align]
ZHOU, Ran

题目大意:

将消息放入队列和从队列中提取问题,当输入GET命令式获得对立中的消息,如果队列是空则输出EMPTY QUEUE!,当输入PUT时,是将消息压入队列并输入消息内容、参数和优先级,输出是如果优先级相同则遵循先进先出的规则

解题思路:

本题主要是对优先队列问题的考察,在解决该问题时,首先定义一个结构体,然后利用队列进行操作,当输入为GET是首先判断队列是否为空,如果是空输出TMPTY QUEUE,如果不是空则根据优先级情况输出,当输入PUT时,则将输入信息输入压入队列中。#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node//node的结构体定义
{
char s[20];
int canshu;
int priority;//优先级
int num;//进站顺序
bool operator < (const node&b)
const
{
if(priority==b.priority)
{
return num>b.num;
}
return priority>b.priority;
}
};
int main()
{
int i,j;
char str[20];
priority_queue<node>Q;//优先队列
int tot=0;
while(scanf("%s",str)!=EOF)//输入字符串
{
if(strcmp(str,"GET")==0)//如果字符串与GET相等,相等输出0
{
if(Q.empty())// 如果是空
{
printf("EMPTY QUEUE!\n");
}
else
{
printf("%s %d\n",Q.top().s,Q.top().canshu);//输出
Q.pop();//出队
}
}
else
{
node tmp;
scanf("%s%d%d",tmp.s,&tmp.canshu,&tmp.priority);//输入字符串,参数,优先级
tmp.num=++tot;
Q.push(tmp);//放入队列
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: