题目描述
自定义结构体表示一张扑克牌,包含类型——黑桃、红桃、梅花、方块、王;大小——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;
}
}
}
}