如何按优先级来输出消息

img

img


#include
#include
struct BST{
int w;
char s[110];
}q[400040];

int main()
{
int Size=0,n,i;
char tx[5],ch[5];
int tp;
scanf("%d",&n);

q[0].w=-1000000000;
while(n--)
{
    scanf("%s",tx);
    if(strcmp("PUT",tx)==0)
    {
        scanf("%s%d",ch,&tp);
        i=++Size;
        for(;q[i/2].w>tp;i/=2)
        {
            q[i].w=q[i/2].w;
            strcpy(q[i].s,q[i/2].s);
        }
        q[i].w=tp;
        strcpy(q[i].s,ch);
    }
    else
    {
        if(!Size)
            puts("EMPTY QUEUE!");
        else
        {
            char tt[10];
            printf("%s\n",q[1].s);
            int temp=q[Size].w;
            strcpy(tt,q[Size--].s);
            int child,parent;
            for(parent=1;parent*2<=Size;parent=child)
            {
                child=parent*2;
                if(child!=Size&&(q[child+1].w<q[child].w))
                    child++;
                if(temp<q[child].w)
                    break;
                else
                {
                    q[parent].w=q[child].w;
                    strcpy(q[parent].s,q[child].s);
                }
            }
            q[parent].w=temp;
            strcpy(q[parent].s,tt);
        }
    }
}
return 0;

}

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^