Va 131-the psychic poker player

Source: Internet
Author: User

Question: There are five cards in your hand, and there are a pile of cards (five cards) on the table. You can discard K cards in your hand and then take the top K cards from the pile.

Comparison rules: (sort by priority)

1. Straight-flush: The card surface is T (10)-A, regardless of whether the color is the same;

2. Four-of-a-kind: four items with four identical values on the card surface;

3. Full-house: The ship card with three identical values and the remaining two with the same value;

4. Flush: the colors of the five cards are the same, not the same;

5. Straight: shunzi. The values of the five cards are continuous. A can be either 1 or 14;

6. Three-of-a-kind: Three, with three identical values on the card surface;

7. Two-pairs: two pairs with two pairs;

8. One-Pair: One pair. The card has one pair, that is, two identical values;

9. Highest-card: big-name model.

Analysis: Search and enumeration. The enumerated number of cards can be determined from every case of 0-5, and the highest priority value can be obtained.

Note: Reading is the focus.

# Include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> # define min (x, y) (x) <(y )? (X) :( y) using namespace STD; char temp [5] [3]; char card [10] [3]; int maps [5] [13]; char output [11] [20] = {"", "straight-flush", "Four-of-a-kind", "full-house", "flush ", "straight", "three-of-a-kind", "two-pairs", "one-pair", "highest-card ",""}; int value (char ch) {If (CH = 'T') return 9; If (CH = 'J') return 10; If (CH = 'q ') return 11; If (CH = 'k') return 12; If (CH = 'A') return 0; return ch-'1';} I NT color (char ch) {If (CH = 's') return 0; If (CH = 'H') return 1; if (CH = 'D ') return 2; If (CH = 'C') return 3;} int tests () {// royal-flush | straight-flushfor (INT I = 0; I <5; ++ I) if (maps [I] [0] & maps [I] [9] & maps [I] [10] & maps [I] [11] & maps [I] [12 ]) return 1; // four-of-a-kindfor (INT I = 0; I <13; ++ I) if (maps [4] [I] = 4) return 2; // full-houseint three = 0, two = 0; For (INT I = 0; I <13; ++ I) {If (maps [4] [I] = 2) Two ++; if (maps [4] [I] = 3) Three ++;} If (two & Three) return 3; // flushfor (INT I = 0; I <4; ++ I) {int COUNT = 0; For (Int J = 0; j <13; ++ J) Count + = maps [I] [J]; if (count> = 5) return 4;} // straightfor (INT I = 0; I <10; ++ I) if (maps [4] [I] & maps [4] [I + 1] & maps [4] [I + 2] & maps [4] [I + 3] & maps [4] [(I + 4) % 13]) return 5; // three-of-a-kindif (Th REE) return 6; // two-pairsif (two> 1) return 7; // One-pair Rif (two) return 8; return 9;} void change () {for (INT I = 0; I <5; ++ I) strcpy (temp [I], Card [I + 5]); memset (maps, 0, sizeof (MAPS); For (INT I = 0; I <5; ++ I) {maps [color (temp [I] [1])] [value (temp [I] [0])] = 1; maps [4] [value (temp [I] [0])] ++ ;} int min = tests (); For (int K = 1; k <= 5; ++ K) {int XX, YY, comb = (1 <k)-1; while (comb <32) {// calculate the set Int J = 0, Count = 0, move = 5; do {If (1 <j) & comb) corresponding to the current State) strcpy (temp [count ++], Card [J]); j ++;} while (j <5); While (count <5) strcpy (temp [count ++], Card [move ++]); memset (maps, 0, sizeof (MAPS); For (INT I = 0; I <5; ++ I) {maps [color (temp [I] [1])] [value (temp [I] [0])] = 1; maps [4] [value (temp [I] [0])] ++;} min = min (Min, tests ()); // bitwise operation calculates the next combination of xx = comb &-comb, YY = comb + xx; Comb = (comb &~ YY)/XX> 1) | YY;} printf ("% s \ n", output [Min]);} int main () {While (~ Scanf ("% s", Card [0]) {for (INT I = 1; I <10; ++ I) scanf ("% s ", card [I]); printf ("hand:"); For (INT I = 0; I <5; ++ I) printf ("% s ", card [I]); printf ("deck:"); For (INT I = 5; I <10; ++ I) printf ("% s ", card [I]); printf ("Best hand:"); change () ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.