# Huawei machine test HJ88: Poker size

Author: Zhai Tianbao Steven

### Title Description:

We should all be familiar with the poker game. A deck of cards consists of 54 cards, including 3~A, 4 for 2, 1 for Xiao Wang and 1 for Wang. The face of the card from small to large is represented by the following characters and strings (where lower case Joker represents Xiao Wang and upper case Joker represents Wang Wang):
3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
Input two hands of cards. Connect the two hands with "-". Each card in each hand is separated by a space. There is no space on both sides of "-", such as 4   four   four   4-joker   JOKER.
Please compare the size of two hands and output a larger card. If there is no comparison relationship, output ERROR.
Basic rules:
(1) Input each hand may be one of sub, pair, shunzi (5 consecutive), three, bomb (4) and king. There are no other circumstances. Input ensures that both hands are legal and shunzi has been arranged from small to large;
(2) Except that bombs and King pairs can be compared with all cards, other types of cards can only be compared with the same type (e.g. pair to pair, three to three), regardless of card splitting (e.g. splitting pairs into children);
(3) The size rules are the same as the common rules we usually know. The size of the card is compared with the size of the sub, pair and pair; Shunzi compares the minimum card size; The bomb is larger than all the cards in front, and the size of the card is compared between the bombs; The king is the biggest card;

(4) The two hands entered will not be equal.

### Enter Description:

Input two hands of cards. Connect the two hands with "-". Each card in each hand is separated by a space. There is no space on both sides of "-", such as   four   four   four   4-joker   JOKER.

### Output Description:

Output the larger hand of the two hands, excluding the connector. The order of playing cards remains unchanged and is still separated by spaces; If there is no comparison relationship, ERROR is output.

### Example:

Input:

```4 4 4 4-joker JOKER
```

Output:

`joker JOKER`

### Problem solving ideas:

This is a string analysis problem. First, read the string, represent the two hands with s1 and s2 respectively, and Compare the output results with the Compare function; First judge the situation of the king, and then disassemble the hand with the getchild function to obtain the number of hands; Then, the isBomb function is used to analyze the bomb situation. If there is a bomb, it is large, and if there is the same bomb, the dictionary card is used to judge the size; If there is no bomb, analyze whether the number of cards in hand is consistent. If it is inconsistent, it cannot be compared. If it is consistent, use card to judge the size. complete.

### Test code:

```#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Card set
vector<string> card={
"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"
};
// Compare size
bool cmd(string s1,string s2)
{
int m,n;
for(int i=0;i<card.size();++i)
{
if(s1==card[i])
m=i;
if(s2==card[i])
n=i;
}
if(m>n)
return true;
else
return false;
}
// Get cards
vector<string> getchild(string s)
{
vector<string> result;
string ss;
for(int i=0;i<s.size();++i)
{
if(s[i]==' ')
{
result.push_back(ss);
ss.clear();
continue;
}
ss+=s[i];
}
result.push_back(ss);
return result;
}
// Judge whether the cards are consistent
bool isSame(vector<string> child)
{
for(int i=1;i<child.size();++i)
{
if(child[i]!=child[0])
{
return false;
}
}
return true;
}
// Determine whether it is a bomb
bool isBomb(string s)
{
vector<string> child=getchild(s);
if(child.size()==4&&isSame(child))
return true;
return false;
}
// Judge size
string Compare(string s1,string s2)
{
// Judge whether there is a right to the king
if(s1=="joker JOKER")
return s1;
else if(s2=="joker JOKER")
return s2;
vector<string> c1=getchild(s1);
vector<string> c2=getchild(s2);

// Determine whether there is a bomb
if(isBomb(s1)&&(!isBomb(s2)))
return s1;
else if(isBomb(s2)&&(!isBomb(s1)))
return s2;
else if(isBomb(s1)&&isBomb(s2)){
if(cmd(c1[0],c2[0]))
return s1;
else
return s2;
}
// If it's not a king or a bomb, judge the size again
else{
if(c1.size()!=c2.size())
return "ERROR";
if(cmd(c1[0],c2[0]))
return s1;
else
return s2;
}
}

int main()
{
string str;
while(getline(cin,str))
{
string s1,s2;
bool first=true;
for(int i=0;i<str.size();++i)
{
if(str[i]=='-')
{
first=false;
continue;
}
if(first)
s1+=str[i];
else
s2+=str[i];
}
cout<<Compare(s1,s2)<<endl;
}
return 0;
}```

Tags: C++

Posted on Thu, 07 Oct 2021 23:06:40 -0400 by BrazilMac