• 7-26 Windows消息队列(25 分)(堆排序)


    7-26 Windows消息队列(25 分)

    消息队列是Windows系统的基础。对于每个进程,系统维护一个消息队列。如果在进程中有特定事件发生,如点击鼠标、文字改变等,系统将把这个消息加到队列当中。同时,如果队列不是空的,这一进程循环地从队列中按照优先级获取消息。请注意优先级值低意味着优先级高。请编辑程序模拟消息队列,将消息加到队列中以及从队列中获取消息。

    输入格式:

    输入首先给出正整数N(105​​),随后N行,每行给出一个指令——GETPUT,分别表示从队列中取出消息或将消息添加到队列中。如果指令是PUT,后面就有一个消息名称、以及一个正整数表示消息的优先级,此数越小表示优先级越高。消息名称是长度不超过10个字符且不含空格的字符串;题目保证队列中消息的优先级无重复,且输入至少有一个GET

    输出格式:

    对于每个GET指令,在一行中输出消息队列中优先级最高的消息的名称和参数。如果消息队列中没有消息,输出EMPTY QUEUE!。对于PUT指令则没有输出。

    输入样例:

    9
    PUT msg1 5
    PUT msg2 4
    GET
    PUT msg3 2
    PUT msg4 4
    GET
    GET
    GET
    GET
    

    输出样例:

    msg2
    msg3
    msg4
    msg1
    EMPTY QUEUE!


    解题思路:本题主要是建立一个小顶堆,难一点的是输出后的重建,需要模拟一下过程
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 
     5 typedef struct Node *node;
     6 struct Node
     7 {
     8     char mes[11];
     9     int priority;
    10 };
    11 
    12 struct
    13 {
    14     node heap[100005];
    15     int num;
    16 } Heap;
    17 
    18 void Put();
    19 void Get();
    20 
    21 int main()
    22 {
    23     int n;
    24     scanf("%d",&n);
    25     Heap.heap[0] = (node)malloc( sizeof(struct Node));
    26     Heap.heap[0]->priority = -1;
    27     Heap.num = 0;
    28 
    29     while( n--)
    30     {
    31         char op[4];
    32         getchar();
    33         scanf("%s",op);
    34         switch( op[0])
    35         {
    36         case 'P' :
    37             Put();
    38             break;
    39         case 'G' :
    40             Get();
    41             break;
    42         default :
    43             break;
    44         }
    45     }
    46 
    47     return 0;
    48 }
    49 
    50 
    51 void Put()
    52 {
    53     //读入数据,建立一个小顶堆
    54     int i;
    55     node temp = ( node ) malloc( sizeof( struct Node));
    56     scanf("%s %d",temp->mes,&temp->priority);
    57     for( i=++Heap.num; Heap.heap[i/2]->priority > temp->priority; i=i/2)
    58     {
    59         Heap.heap[i] = Heap.heap[i/2];
    60     }
    61     Heap.heap[i] = temp;
    62 }
    63 
    64 void Get()
    65 {
    66     //输出数据,重建顶堆
    67     int i;
    68 
    69     if( Heap.num<1)
    70     {
    71         printf("EMPTY QUEUE!
    ");
    72         return ;
    73     }
    74     printf("%s
    ",Heap.heap[1]->mes);
    75     for( i=1; i*2<Heap.num; )
    76     {
    77         if( i*2+1<Heap.num && Heap.heap[i*2+1]->priority<Heap.heap[i*2]->priority)
    78         {
    79             //如果有两个根节点,并且右结点优先数小于左结点优先数
    80             if( Heap.heap[i*2+1]->priority<Heap.heap[Heap.num]->priority)
    81             {
    82                 Heap.heap[i] = Heap.heap[i*2+1];
    83                 i=i*2+1;
    84             }
    85             else break;
    86         }
    87         else
    88         {
    89             if(Heap.heap[i*2]->priority < Heap.heap[Heap.num]->priority)
    90             {
    91                 Heap.heap[i] = Heap.heap[i*2];
    92                 i *= 2;
    93             }
    94             else break;
    95         }
    96     }
    97     Heap.heap[i] = Heap.heap[Heap.num--]; //将最后的一个元素补在空缺
    98 }
    
    
    
     
    在这个国度中,必须不停地奔跑,才能使你保持在原地。如果想要寻求突破,就要以两倍现在速度奔跑!
  • 相关阅读:
    [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1
    致鸡鸣狗盗
    一个技术人的知识管理方法论
    一碗清酒聚知音 我看 《少年黄飞鸿-铁马骝》
    [Erlang 0115] 2014值得期待的Erlang两本新书
    [Erlang 0114] Erlang Resources 小站 2013年7月~12月资讯合集
    我的2013 Q.E.D
    Elixir
    [Erlang 0113] Elixir 编译流程梳理
    [Erlang 0112] Elixir Protocols
  • 原文地址:https://www.cnblogs.com/yuxiaoba/p/8412567.html
Copyright © 2020-2023  润新知