[UVA][牌類遊戲] 10315 - Poker Hands
Problem D
Poker Hands
Input: standard input
Output: standard output
Time Limit: 2 seconds
Memory Limit: 32 MB
A poker deck contains 52 cards - each card has a suit which is one of clubs, diamonds, hearts, or spades (denoted C, D, H, and S in the input data). Each card also has a value which is one of 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace (denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A). For scoring purposes, the suits are unordered while the values are ordered as given above, with 2 being the lowest and ace the highest value.
A poker hand consists of 5 cards dealt from the deck. Poker hands are ranked by the following partial order from lowest to highest
- High Card: Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on.
- Pair: 2 of the 5 cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order.
- Two Pairs: The hand contains 2 different pairs. Hands which both contain 2 pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card.
- Three of a Kind: Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the 3 cards.
- Straight: Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card.
- Flush: Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.
- Full House: 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.
- Four of a kind: 4 cards with the same value. Ranked by the value of the 4 cards.
- Straight flush: 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand.
Your job is to compare several pairs of poker hands and to indicate which, if either, has a higher rank.
Input
The input file contains several lines, each containing the designation of 10 cards: the first 5 cards are the hand for the player named "Black" and the next 5 cards are the hand for the player named "White".
Output
For each line of input, print a line containing one of the following three lines:
Black wins.
White wins.
Tie.
Sample Input
2H 3D 5S 9C KD 2C 3H 4S 8C AH
2H 4S 4C 2D 4H 2S 8S AS QS 3S
2H 3D 5S 9C KD 2C 3H 4S 8C KH
2H 3D 5S 9C KD 2D 3H 5C 9S KH
Sample Output
White wins.
Black wins.
Black wins.
Tie.
(The Decider Contest, Source: Waterloo ACM Programming Contest)
還是讓我死了吧,這種題目真的寫起來會崩潰。
#include <stdio.h>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
int s2n(char c) {
if(c >= '0' && c <= '9')
return c-'0';
if(c == 'A') return 14;
if(c == 'T') return 10;
if(c == 'J') return 11;
if(c == 'Q') return 12;
if(c == 'K') return 13;
}
char b[5], w[5], fb[5], fw[5];
int solve() {
sort(b, b+5), sort(w, w+5);
sort(fb, fb+5), sort(fw, fw+5);
int BB = 0, WW = 0;
int a1, a2, b1, b2, c1, c2, d1, d2;
// straight flush
if(fb[0] == fb[4] && b[0]+4 == b[4])
BB = b[4];
if(fw[0] == fw[4] && w[0]+4 == w[4])
WW = w[4];
//printf("straight flush %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB && WW) return 0;
// four of a kind
if(b[0] == b[3] || b[1] == b[4])
BB = b[1];
if(w[0] == w[3] || w[1] == w[4])
WW = w[1];
//printf("four of a kind %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB && WW) return 0;
// full house
if(b[0] == b[2] && b[3] == b[4])
BB = b[0];
if(b[0] == b[1] && b[2] == b[4])
BB = b[2];
if(w[0] == w[2] && w[3] == w[4])
WW = w[0];
if(w[0] == w[1] && w[2] == w[4])
WW = w[2];
//printf("full house %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB && WW) return 0;
// flush
if(fb[0] == fb[4])
BB = 1;
if(fw[0] == fw[4])
WW = 1;
//printf("fush %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB == 1 && WW == 1) {
for(int i = 4; i >= 0; i--) {
if(b[i] > w[i]) return 1;
if(w[i] > b[i]) return -1;
}
return 0;
}
// straight
if(b[0]+4 == b[4])
BB = b[4];
if(w[0]+4 == w[4])
WW = w[4];
//printf("straight %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB && WW) return 0;
// three of a kind
for(int i = 0; i < 3; i++)
if(b[i] == b[i+2])
BB = b[i];
for(int i = 0; i < 3; i++)
if(w[i] == w[i+2])
WW = w[i];
//printf("three of a kind %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB && WW) return 0;
// two pairs
if(b[0] == b[1] && b[2] == b[3])
a1 = b[0], b1 = b[2], c1 = b[4], BB = 1;
if(b[0] == b[1] && b[3] == b[4])
a1 = b[0], b1 = b[3], c1 = b[2], BB = 1;
if(b[1] == b[2] && b[3] == b[4])
a1 = b[1], b1 = b[3], c1 = b[0], BB = 1;
if(w[0] == w[1] && w[2] == w[3])
a2 = w[0], b2 = w[2], c2 = w[4], WW = 1;
if(w[0] == w[1] && w[3] == b[4])
a2 = w[0], b2 = w[3], c2 = w[2], WW = 1;
if(w[1] == w[2] && w[3] == b[4])
a2 = w[1], b2 = w[3], c2 = w[0], WW = 1;
//printf("two pair %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB && WW) {
if(b1 != b2) return b1 > b2 ? 1 : -1;
if(a1 != a2) return a1 > a2 ? 1 : -1;
if(c1 > c2) return 1;
if(c1 < c2) return -1;
return 0;
}
// one pair
if(b[0] == b[1])
BB = 1, a1 = b[0], b1 = b[2], c1 = b[3], d1 = b[4];
if(b[1] == b[2])
BB = 1, a1 = b[1], b1 = b[0], c1 = b[3], d1 = b[4];
if(b[2] == b[3])
BB = 1, a1 = b[2], b1 = b[0], c1 = b[1], d1 = b[4];
if(b[3] == b[4])
BB = 1, a1 = b[3], b1 = b[0], c1 = b[1], d1 = b[2];
if(w[0] == w[1])
WW = 1, a2 = w[0], b2 = w[2], c2 = w[3], d2 = w[4];
if(w[1] == w[2])
WW = 1, a2 = w[1], b2 = w[0], c2 = w[3], d2 = w[4];
if(w[2] == w[3])
WW = 1, a2 = w[2], b2 = w[0], c2 = w[1], d2 = w[4];
if(w[3] == w[4])
WW = 1, a2 = w[3], b2 = w[0], c2 = w[1], d2 = w[2];
//printf("one pair %d %d\n", BB, WW);
if(BB > WW) return 1;
if(BB < WW) return -1;
if(BB && WW) {
if(a1 != a2) return a1 > a2 ? 1 : -1;
if(d1 != d2) return d1 > d2 ? 1 : -1;
if(c1 != c2) return c1 > c2 ? 1 : -1;
if(b1 != b2) return b1 > b2 ? 1 : -1;
if(a1 != a2) return a1 > a2 ? 1 : -1;
return 0;
}
for(int i = 4; i >= 0; i--) {
if(b[i] > w[i]) return 1;
if(w[i] > b[i]) return -1;
}
return 0;
}
int main() {
int i, j, k;
char s[1024], B[5][3], W[5][3];
while(1) {
for(i = 0; i < 5; i++) {
if(scanf("%s", B[i]) == 1);
else return 0;
b[i] = s2n(B[i][0]), fb[i] = B[i][1];
}
for(i = 0; i < 5; i++) {
scanf("%s", W[i]);
w[i] = s2n(W[i][0]), fw[i] = W[i][1];
}
int flag = solve();
if(flag == 1)
puts("Black wins.");
else if(flag == -1)
puts("White wins.");
else
puts("Tie.");
}
return 0;
}