优先级队列的应用 之对扑克牌排序

本文探讨了如何使用优先级队列作为排序工具,以高效地对扑克牌进行排序。通过理解优先级队列的工作原理,我们可以利用其特性在O(n log n)的时间复杂度内完成排序。这一方法对于处理大量数据或需要实时更新排序的场景非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <queue>
#include <functional>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node
{
   char color;
   char data;
   int i;
   node( char a= '0', char b= '0',int c=0 ):
        color(a), data(b),i(c) {};
   friend bool operator <(const node &a,const node &b)
   {
       return a.i>b.i;
   }

};
char s[2];
char ans [105][3];
priority_queue<node> q[13];
int main()
{
    int n;
    cin >> n;

    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        char a=s[0];char b=s[1];
        q[b-'0'-1].push(node(a,b,i));
    }
    int j=0;
    for(int i=0;i<9;i++)
    {
        printf("Queue%d:",i+1);
        int flag=1;
        while(!q[i].empty())
        {
            if(flag==1)
            {
                printf("%c%c",q[i].top().color,q[i].top().data);
                flag=0;
            }
            else
            {
                printf(" %c%c",q[i].top().color,q[i].top().data);
            }
           char a= q[i].top().color;char b=q[i].top().data;
           q[a-'A'+9].push(node(a,b,j++));
           q[i].pop();
        }
        printf("\n");
    }
    j=0;
    for(int i=9;i<13;i++)
    {
        printf("Queue%c:",'A'+i-9);
        int flag=1;
        while(!q[i].empty())
        {
            if(flag)
                {
                printf("%c%c",q[i].top().color,q[i].top().data);
                flag=0;
                }
            else
                printf(" %c%c",q[i].top().color,q[i].top().data);
            ans[j][0]=q[i].top().color;
            ans[j][3]='\0';
            ans[j++][1]=q[i].top().data;

            q[i].pop();
        }
        cout <<endl;
    }
    for(int i=0;i<j;i++)
    {
        if(i==0)
        cout<<ans[i];
        else
            cout << ' '<<ans[i];
    }
    cout << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值