题目
分析
这是一道模拟题,一步一步分析来走:
首先定义一个结构体people
{
name//人名
xu//一手牌的规则序号
key1,key2,key2//表示如果都是那一种一手牌,按关键字排序
}
有了上面的定义,我们只需要对每一个输入的人,分析他手中的一手牌是哪个规则序号(1:大牌 ;2:对子…),然后将这种牌的排序关键字记录到结构体里面(比如说是1大牌,关键字1就是5张牌的总和),这些用solve()函数解决。
解决完之后,就是多关键字排序的问题了。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct people{
char name[20];
int xu,key1,key2,key3;
bool operator <(const people &P){
if(xu!=P.xu) return xu>P.xu;//升序
else if(key1!=P.key1) return key1>P.key1;//升序
else if(key2!=P.key2) return key2>P.key2;//升序
else if(key3!=P.key3) return key3>P.key3;//升序
else return strcmp(name, P.name)<=0;
}
}thep[100005];
int wu[5];
void jiexi(char* S){
int j=0,len=strlen(S);
for(int i=0;i<len;i++){
if(S[i]=='A') wu[j]=1;
else if(S[i]=='1') wu[j]=10,i++;
else if(S[i]=='J') wu[j]=11;
else if(S[i]=='Q') wu[j]=12;
else if(S[i]=='K') wu[j]=13;
else wu[j]=S[i]-'0';
j++;
}
}
void solve(int i){
sort(wu,wu+5);
if(wu[0]==1&&wu[1]==10&&wu[2]==11&&wu[3]==12&&wu[4]==13){//龙顺
thep[i].xu=8;
}
else if(wu[4]==wu[3]+1&&wu[3]==wu[2]+1&&wu[2]==wu[1]+1&&wu[1]==wu[0]+1){//顺子
thep[i].xu=7;thep[i].key1=wu[4];
}
else if((wu[0]==wu[1]&&wu[1]==wu[2]&&wu[2]==wu[3])||(wu[1]==wu[2]&&wu[2]==wu[3]&&wu[3]==wu[4])){
thep[i].xu=6;if(wu[0]!=wu[1]){
thep[i].key1=wu[1];thep[i].key2=wu[0];
}else{
thep[i].key1=wu[1];thep[i].key2=wu[4];
}
}
else if((wu[0]==wu[1]&&wu[2]==wu[3]&&wu[3]==wu[4])||wu[0]==wu[1]&&wu[1]==wu[2]&&wu[3]==wu[4]){
thep[i].xu=5;if(wu[2]==wu[0]){
thep[i].key1=wu[0];thep[i].key2=wu[4];
}else{
thep[i].key1=wu[4];thep[i].key2=wu[0];
}
}
else if((wu[0]==wu[1]&&wu[0]==wu[2])||(wu[1]==wu[2]&&wu[2]==wu[3])||(wu[2]==wu[3]&&wu[3]==wu[4])){
thep[i].xu=4;
if(wu[0]==wu[1]){
thep[i].key1=wu[0];thep[i].key2=wu[3]+wu[4];
}else if(wu[3]==wu[4]){
thep[i].key1=wu[4];thep[i].key2=wu[0]+wu[1];
}else{
thep[i].key1=wu[1];thep[i].key2=wu[0]+wu[4];
}
}
else if((wu[0]==wu[1]&&wu[2]==wu[3])||(wu[0]==wu[1]&&wu[3]==wu[4])||(wu[1]==wu[2]&&wu[3]==wu[4])){
thep[i].xu=3;if(wu[3]!=wu[4]){
thep[i].key1=wu[2];thep[i].key2=wu[0];thep[i].key3=wu[4];
}else if(wu[1]!=wu[2]){
thep[i].key1=wu[3];thep[i].key2=wu[0];thep[i].key3=wu[2];
}else{
thep[i].key1=wu[3];thep[i].key2=wu[1];thep[i].key3=wu[0];
}
}
else if(wu[0]==wu[1]||wu[1]==wu[2]||wu[2]==wu[3]||wu[3]==wu[4]){
thep[i].xu=2;if(wu[0]==wu[1]){
thep[i].key1=wu[0];thep[i].key2=wu[2]+wu[3]+wu[4];
}else if(wu[1]==wu[2]){
thep[i].key1=wu[1];thep[i].key2=wu[0]+wu[3]+wu[4];
}else if(wu[2]==wu[3]){
thep[i].key1=wu[2];thep[i].key2=wu[0]+wu[1]+wu[4];
}else{
thep[i].key1=wu[3];thep[i].key2=wu[0]+wu[1]+wu[2];
}
}
else{
thep[i].xu=1;thep[i].key1=wu[0]+wu[1]+wu[2]+wu[3]+wu[4];
}
}
int main()
{
int n;
while(~scanf("%d",&n)){
char puke[20];
for(int i=0;i<n;i++){
//初始化
thep[i].key1=0;thep[i].key2=0;thep[i].key3=0;
scanf("%s %s",&thep[i].name,&puke);
jiexi(puke);
solve(i);
}
sort(thep,thep+n);
for(int i=0;i<n;i++){
//cout<<thep[i].xu<<" "<<thep[i].key1<<" "<<thep[i].key2<<" "<<thep[i].key3<<" ";
cout<<thep[i].name<<endl;
}
}
return 0;
}