C
来源:互联网 发布:帝国时代2知乎 编辑:程序博客网 时间:2024/05/29 15:11
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.
GETPUT msg1 10 5PUT msg2 10 4GETGETGET
EMPTY QUEUE!msg2 10msg1 10EMPTY QUEUE!
思路:按优先队列写一下就好了:
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<queue> using namespace std; struct node { char name[50]; int x,y,t; bool operator < (const node &a) const { if(a.t!=t) return a.t<t; return a.y<y; } }s; char p[10]; int main(){ priority_queue<node> que; while(!que.empty() ) que.pop(); memset(p,0,sizeof(p)); int k=1; while(scanf("%s",p)!=EOF) { memset(s.name,0,sizeof(s.name)); if(!strcmp(p,"PUT")) { scanf("%s %d %d",s.name ,&s.x ,&s.t ); s.y=k; que.push(s); k++; } else { if(!que.empty() ) { printf("%s %d\n",que.top().name,que.top().x); que.pop(); } else puts("EMPTY QUEUE!"); } } return 0; }