The solution to the problem of Texas Hold’em

Time:2021-5-7

Title Description

Recently, aqua is a big fan of Texas Hold’em. So she found a lot of people to play with. Because of the large number of people, aqua has to change the rules of the game:

  1. All playing cards only look at the numbers, not the colors.
  2. The value of each card is one of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 (corresponding to a, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K)
  3. Each player draws five cards from a complete set of playing cards (no king or King), and the possible hand valueFrom low to highThey are arranged as follows:
  • High card: a card that does not contain the following cards. For all high cards, according to the value of the five cards and from large to small order.
  • Pair: there are 2 cards of the same value out of 5 cards in hand. For the cards that all have pairs, they are sorted from large to small according to the value of the cards that make up the pair. If all of these are the same, the order is from large to small according to the sum of the values of the remaining three cards in the hand**
  • Two pairs: two different pairs in hand. For a hand that contains two pairs of cards, it is sorted from large to small according to the value of the highest pair. If the highest pair is the same, the value of the other pair is sorted from large to small. If these values are the same, they are sorted according to the value of the remaining cards.
  • Three: have 3 cards of the same value in your hand. For a hand that contains three cards, it is sorted from large to small according to the value of the cards that make up the three cards. If these values are the same, they are sorted according to the value of the remaining cards.
  • Full house: in the hands of a three and a pair. In the same way, sort by three sizes first. If the three sizes are the same, sort by sub size.
  • Four: you have four cards of the same value in your hand. For a hand that contains four cards, it is sorted from large to small according to the value of the cards that make up the four cards. If these values are the same, they are sorted according to the value of the remaining cards.
  • Shunzi: I have 5 consecutive cards in my hand. For the hands that all contain shunzi, they are sorted according to the largest shunzi.
  • Royal Flush: hands 10 to a (10, J, Q, K, a). It’s the biggest hand!

Now that Aqua knows everyone’s hand, she wants to know everyone’s ranking list. If the player’s hand size is equal, it will be output according to the dictionary order of the player’s name. Make sure there are no duplicate names. Can you help her?

Input format:
The first line contains a positive integer n (1 < = n < = 100000), indicating the number of players.
Next N lines, each line contains two strings: m (1 < = | m | < = 10), indicating the name of the player; S (1 < = | s < = 10) indicates the player’s hand.

Output format:
Output the ranking list of N players.

Thinking of solving problems

1. Conversion card surface: convert character array to number array
2. Sort the deck of each deck, classify and judge various types of cards, and calculate the corresponding score: (key steps)

  • The card type is divided into level 1-8 from small to large
  • At the same level, each card is given different priority to calculate the score
  • For example, two pairs of 22335 have a score of 310000+2100+5=30205

3. Sort by card type, score and name: adopt the method of structure array quick sort

Complete code

#include
#include
#include 
using namespace std;

char num[15] = "0A234567891JQK";
struct member {
	char name[15];
	char card1[15];
	int card2[15];
	int level,score,two1,two2,three,four;
}man[120000];

void convert(char str[], int x[]) {
	int i, j, k;
	for (i = 0, j = 0; str[i] != '
#include
#include
#include 
using namespace std;
char num[15] = "0A234567891JQK";
struct member {
char name[15];
char card1[15];
int card2[15];
int level,score,two1,two2,three,four;
}man[120000];
void convert(char str[], int x[]) {
int i, j, k;
for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
for (k = 1;; k++) {
if (num[k] == str[i]) {
x[j] = k;
break;
}
}
if (x[j] == 10) {
i++;
}
}
}/*Function: change the card surface 
Parameters: STR is character card surface, X is number card surface*/
void judge(member a,int x) {
int i;
for (i = 0; i < 5; i++) {
a.score+= a.card2[i];
}
for (i = 0; i<4; i++) {
if (a.card2[i] == a.card2[i + 1]) {
a.two1= a.card2[i];
}
if (a.card2[i] == a.card2[i + 2]) {
a.three = a.card2[i];
}
if (a.card2[i] == a.card2[i + 3]) {
a.four = a.card2[i];
}
}
for (i = 0; i < 4; i++) {
if (a.card2[i] == a.card2[i + 1]) {
a.two2 = a.card2[i];
break;
}
}
if (a.four != 0) {
a.level = 6;
a.score += a.four * 100 - 4 * a.four;
}
else if (a.three != 0 && a.two1 != a.two2) {
a.level = 5;
a.score += a.three * 100 - 3*a.three;
}
else if (a.three != 0&&a.two1==a.two2) {
a.level = 4;
a.score += a.three * 100 - 3 * a.three;
}
else if (a.two1 != a.two2) {
a.level = 3;
a.score += a.two1 * 10000 + a.two2 * 100 - 2 * a.two1 - 2 * a.two2;
}
else if (a.two1 != 0) {
a.level = 2;
a.score += a.two1 * 100 - a.two1 * 2;
}
else if (a.card2[0] == 1 && a.card2[1] == 10 && a.card2[2] == 11 && a.card2[3] == 12 && a.card2[4] == 13) {
a.level = 8;
}
else if (a.card2[1]==a.card2[0]+1&& a.card2[2] == a.card2[1] + 1 && a.card2[3] == a.card2[2] + 1 && a.card2[4] == a.card2[3] + 1 ) {
a.level = 7;
}
man[x].level = a.level;
man[x].score = a.score;
}/*Classify and judge various types of cards, and calculate the corresponding score*/
bool cmp(member x, member y)
{
if (x.level != y.level) return x.level > y.level;
if (x.score != y.score) return x.score > y.score;
return strcmp(x.name, y.name)<0;
}/*Function: sort by card type, score and name in non ascending order*/
int main()
{
int N, i, j = 0;
scanf("%d", &N);
for (i = 0; i < N; i++) {
scanf("%s%s", man[i].name, man[i].card1);
convert(man[i].card1, man[i].card2);/* Change deck*/
sort(man[i].card2, man[i].card2+5);/* Sort each deck in non ascending order*/
judge(man[i],i);/* Classify and judge various types of cards, and calculate the corresponding score*/
}
sort(man, man+N, cmp);/* Sort by card type, score and name in non ascending order*/
for (i = 0; i < N; i++) {
puts(man[i].name);
}
return 0;
}
'; i++, j++) { for (k = 1;; k++) { if (num[k] == str[i]) { x[j] = k; break; } } if (x[j] == 10) { i++; } } }/*Function: change the card surface Parameters: STR is character card surface, X is number card surface*/ void judge(member a,int x) { int i; for (i = 0; i < 5; i++) { a.score+= a.card2[i]; } for (i = 0; i<4; i++) { if (a.card2[i] == a.card2[i + 1]) { a.two1= a.card2[i]; } if (a.card2[i] == a.card2[i + 2]) { a.three = a.card2[i]; } if (a.card2[i] == a.card2[i + 3]) { a.four = a.card2[i]; } } for (i = 0; i < 4; i++) { if (a.card2[i] == a.card2[i + 1]) { a.two2 = a.card2[i]; break; } } if (a.four != 0) { a.level = 6; a.score += a.four * 100 - 4 * a.four; } else if (a.three != 0 && a.two1 != a.two2) { a.level = 5; a.score += a.three * 100 - 3*a.three; } else if (a.three != 0&&a.two1==a.two2) { a.level = 4; a.score += a.three * 100 - 3 * a.three; } else if (a.two1 != a.two2) { a.level = 3; a.score += a.two1 * 10000 + a.two2 * 100 - 2 * a.two1 - 2 * a.two2; } else if (a.two1 != 0) { a.level = 2; a.score += a.two1 * 100 - a.two1 * 2; } else if (a.card2[0] == 1 && a.card2[1] == 10 && a.card2[2] == 11 && a.card2[3] == 12 && a.card2[4] == 13) { a.level = 8; } else if (a.card2[1]==a.card2[0]+1&& a.card2[2] == a.card2[1] + 1 && a.card2[3] == a.card2[2] + 1 && a.card2[4] == a.card2[3] + 1 ) { a.level = 7; } man[x].level = a.level; man[x].score = a.score; }/*Classify and judge various types of cards, and calculate the corresponding score*/ bool cmp(member x, member y) { if (x.level != y.level) return x.level > y.level; if (x.score != y.score) return x.score > y.score; return strcmp(x.name, y.name)<0; }/*Function: sort by card type, score and name in non ascending order*/ int main() { int N, i, j = 0; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%s%s", man[i].name, man[i].card1); convert(man[i].card1, man[i].card2);/* Change deck*/ sort(man[i].card2, man[i].card2+5);/* Sort each deck in non ascending order*/ judge(man[i],i);/* Classify and judge various types of cards, and calculate the corresponding score*/ } sort(man, man+N, cmp);/* Sort by card type, score and name in non ascending order*/ for (i = 0; i < N; i++) { puts(man[i].name); } return 0; }

Experience

The key of this problem is to classify and judge all kinds of cards, and the premise of classification is to “understand the rules”. In my opinion, using pen and paper or notepad to thoroughly understand the rules of the topic should be the first step.
Under complex rules, this problem needs a large amount of code, and the judgment of various types of cards is prone to cross errors. We can try to block the program into several unrelated modules, and then break through each module one by one.
In terms of test data, you can consider writing your own test data. Taking different cards under different levels as an example, the corresponding level and score can be output during the test, then the problem can be seen directly, and the module can be returned for modification.

Attached:
Online tool for pasting code into word: planetb
How to insert code gracefully in Microsoft Word