Texas poker problem solving

Title Description
Recently, aqua has become addicted to Texas poker. So she found a lot of people to play with her. Due to the large number of people, aqua has to change the rules of the game:

  1. All playing cards only look at numbers, not 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 playing cards from a complete set of playing cards (no king or King), and the possible hand values are arranged from low to high as follows:
  • High card: a card that does not contain the following cards. For all high cards, the sum of the values of the five cards is sorted from large to small.
  • Pair: there are 2 cards of the same value in the 5 cards in hand. For all cards that have a pair, rank the cards that make up the pair from large to small. If all of these are the same, the sum of the values of the remaining three cards in the hand is sorted from large to small. * *
  • Two pairs: two different pairs in hand. For a hand that contains both pairs, the highest pair is sorted from the largest to the smallest. If the highest pair is the same, the other pair is sorted from the largest to the smallest. If these values are the same, the remaining cards are sorted from large to small.
  • Three: three cards of the same value in hand. For all hands that contain three cards, rank them from large to small according to the values of the cards that make up the three cards. If these values are the same, the remaining cards are sorted from large to small.
  • Man tanghong: he has a pair of three in his hand. In the same way, sort by three sizes first. If the three sizes are the same, sort by sub size.
  • Four: four cards of the same value in hand. For all hands containing four cards, they are sorted according to the values of the four cards. If these values are the same, the remaining cards are sorted from large to small.
  • Shunzi: 5 consecutive cards in hand. For all hands containing shunzi, the cards with the largest shunzi are sorted.
  • 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 hands are the same size, they will be output in 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), representing the player's name; s (1 < = | s| = 10), representing the player's hand.

Output format:
Output the ranking list of N players.

Solving problems

1. Convert deck: convert character array to number array
2. Rank each deck, classify and judge each deck type, and calculate the corresponding score: (key steps)

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

3. Sorting by brand type, score and name: fast sorting by structure array

Complete code

#include<stdio.h>
#include<string.h>
#include <algorithm>
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: switch deck 
 Parameters: str is character deck, x is number deck*/

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 brand types, and calculate corresponding scores*/

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 brand 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);/*Conversion deck*/
        sort(man[i].card2, man[i].card2+5);/*Non ascending order of each deck*/
        judge(man[i],i);/*Classify and judge various brand types, and calculate corresponding scores*/
    }
    sort(man, man+N, cmp);/*Sort by brand 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 question is to classify and judge all kinds of cards, and the premise of classification is to read the rules. In my opinion, using pen and paper or notepad to understand the rules of the topic thoroughly should be the first step.
Under the complex rules, the problem needs a lot of code, and it is easy to cross error in the judgment of various card types. We can try to divide the program into several modules, and break through each module one by one.
In terms of test data, you can consider writing your own test data. Take the different cards under different levels as an example, when testing, output the corresponding level and score, you can see the problem directly, and return to the module for modification.

Attachment:
Paste code to word online tool: PlanetB
Use tutorial - > How to insert code in Microsoft word gracefully

Tags: C

Posted on Sat, 07 Mar 2020 08:55:46 -0800 by alexu'