C. 扑克牌排序(结构体)

题目描述

自定义结构体表示一张扑克牌,包含类型——黑桃、红桃、梅花、方块、王;大小——2,3,4,5,6,7,8,9,10,J,Q,K,A,小王(用0表示)、大王(用1表示)。输入n,输入n张扑克牌信息,从大到小输出它们的排序结果。

假设扑克牌的排序规则是大王、小王为第一大、第二大,剩余52张扑克牌按照先花色后大小排序。

花色:黑桃>红桃>梅花>方块。

大小: A>K>Q>J>>10>9>...>2。

提示:百度sort函数、strstr函数使用。

输入

测试次数t

每组测试数据两行:

第一行:n,表示输入n张扑克牌

第二行:n张扑克牌信息,格式见样例

输出

对每组测试数据,输出从大到小的排序结果

输入样例1

3
5
黑桃4 红桃10 梅花Q 方块K 黑桃A
10
大王 梅花10 红桃K 方块9 黑桃8 梅花A 方块Q 小王 黑桃2 黑桃J
5
红桃K 梅花K 黑桃K 方块K 小王
 

输出样例1

黑桃A 黑桃4 红桃10 梅花Q 方块K
大王 小王 黑桃J 黑桃8 黑桃2 红桃K 梅花A 梅花10 方块Q 方块9
小王 黑桃K 红桃K 梅花K 方块K

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct puke{
    char type[9];
    int flower;
    int number;
};
void fcmp(puke *a){
    char p[]="黑桃";
    if(strstr(a->type,p)!=NULL){
        a->flower=4;
    }
    else if(strstr(a->type,"红桃")){
        a->flower=3;
    }
    else if(strstr(a->type,"梅花")){
        a->flower=2;
    }
    else if(strstr(a->type,"方块")){
        a->flower=1;
    }
    if(strstr(a->type,"大王")){
        a->flower=6;//a->type[2]='\0';
    }
    else if(strstr(a->type,"小王")){
        a->flower=5;//a->type[2]='\0';
    }
}
void tcmp(puke *a){
    if(strstr(a->type,"10")){
        a->number=10;
    }
    else{
        //a->type[3]='\0';
        if(strstr(a->type,"A")){
            a->number=14;
        }
        else if(strstr(a->type,"K")){
            a->number=13;
        }
        else if(strstr(a->type,"Q")){
            a->number=12;
        }
        else if(strstr(a->type,"J")){
            a->number=11;
        }
        else if(strstr(a->type,"9")){
			a->number=9;
		}
		else if(strstr(a->type,"8")){
			a->number=8;
		}
		else if(strstr(a->type,"7")){
			a->number=7;
		}
		else if(strstr(a->type,"6")){
			a->number=6;
		}
		else if(strstr(a->type,"5")){
			a->number=5;
		}
		else if(strstr(a->type,"4")){
			a->number=4;
		}
		else if(strstr(a->type,"3")){
			a->number=3;
		}
		else if(strstr(a->type,"2")){
			a->number=2;
		}
    }

}
bool cmp(puke a,puke b){
    if(a.flower!=b.flower) {
        return a.flower>b.flower;
    }
    else{
        return a.number>b.number;
    }
}
int main(){
    int n;
    cin>>n;
    while(n--){
        int m;
        cin>>m;
        puke* card = new puke[m];
        for(int i = 0; i < m ;i ++){
            cin>>card[i].type;
            fcmp(card+i);
            tcmp(card+i);
        }
        //cout<<card[0].type<<endl;
        sort(card,card+m,cmp);

        for(int i = 0 ; i < m ; i++){
            cout<<card[i].type;
            if(i < m-1){
                cout<<" ";
            }
            else if(i  == m-1 ){
                cout<<endl;
            }
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值