// @BEGIN_OF_SOURCE_CODE /* @JUDGE_ID: 17243NT 131 C++ "Paul's a psychic" */ // Send to judge@uva.es #include #include #ifdef ONLINE_JUDGE #define ins cin #define outs cout #else #define ins fin #define outs fout ifstream fin("myprog.in"); ofstream fout("myprog.out"); #endif typedef struct { int r; int s; } cards; void sorthand(cards hand[5]); int getvalue(cards hand[5]); int findmax(cards largehand[10]); int main() { char c, d; int i; cards fullhand[10]; while(ins >> c) { ins.putback(c); cout << "Hand: "; for(i = 0; i < 5; i++) { ins >> c >> d; cout << c << d << ' '; if(c == 'A') fullhand[i].r = 1; else if(c == 'T') fullhand[i].r = 10; else if(c == 'J') fullhand[i].r = 11; else if(c == 'Q') fullhand[i].r = 12; else if(c == 'K') fullhand[i].r = 13; else fullhand[i].r = c - '0'; fullhand[i].s = d; } cout << "Deck: "; for(; i < 10; i++) { ins >> c >> d; cout << c << d << ' '; if(c == 'A') fullhand[i].r = 1; else if(c == 'T') fullhand[i].r = 10; else if(c == 'J') fullhand[i].r = 11; else if(c == 'Q') fullhand[i].r = 12; else if(c == 'K') fullhand[i].r = 13; else fullhand[i].r = c - '0'; fullhand[i].s = d; } cout << "Best hand: "; i = findmax(fullhand); switch(i) { case 1: cout << "highest-card\n"; break; case 2: cout << "one-pair\n"; break; case 3: cout << "two-pairs\n"; break; case 4: cout << "three-of-a-kind\n"; break; case 5: cout << "straight\n"; break; case 6: cout << "flush\n"; break; case 7: cout << "full-house\n"; break; case 8: cout << "four-of-a-kind\n"; break; case 9: cout << "straight-flush\n"; break; } } return 0; } int findmax(cards largehand[10]) { int p, i, j; int max = 0; cards subhand[5]; for(p = 0; p < 32; p++) { i = 0; if((p >> 4) % 2 == 0) subhand[i++] = largehand[0]; if((p >> 3) % 2 == 0) subhand[i++] = largehand[1]; if((p >> 2) % 2 == 0) subhand[i++] = largehand[2]; if((p >> 1) % 2 == 0) subhand[i++] = largehand[3]; if(p % 2 == 0) subhand[i++] = largehand[4]; j = 5; for(; i < 5; i++) subhand[i] = largehand[j++]; j = getvalue(subhand); if(j > max) max = j; } return max; } int getvalue(cards hand[5]) { sorthand(hand); // Straight flush if((hand[0].s == hand[1].s && hand[1].s == hand[2].s && hand[2].s == hand[3].s && hand[3].s == hand[4].s) && ((hand[2].r == hand[1].r + 1 && hand[3].r == hand[1].r + 2 && hand[4].r == hand[1].r + 3) && (hand[1].r == hand[0].r + 1 || (hand[0].r == 1 && hand[4].r == 13)))) return 9; // Four of a kind if(hand[0].r == hand[3].r || hand[1].r == hand[4].r) return 8; // Full house if((hand[0].r == hand[1].r && hand[2].r == hand[4].r) || (hand[0].r == hand[2].r && hand[3].r == hand[4].r)) return 7; // Flush if(hand[0].s == hand[1].s && hand[1].s == hand[2].s && hand[2].s == hand[3].s && hand[3].s == hand[4].s) return 6; // Straight if((hand[2].r == hand[1].r + 1 && hand[3].r == hand[1].r + 2 && hand[4].r == hand[1].r + 3) && (hand[1].r == hand[0].r + 1 || (hand[0].r == 1 && hand[4].r == 13))) return 5; // Three of a kind if(hand[0].r == hand[2].r || hand[1].r == hand[3].r || hand[2].r == hand[4].r) return 4; // Two pair if((hand[0].r == hand[1].r && (hand[2].r == hand[3].r || hand[3].r == hand[4].r)) || (hand[1].r == hand[2].r && hand[3].r == hand[4].r)) return 3; // One pair if(hand[0].r == hand[1].r || hand[1].r == hand[2].r || hand[2].r == hand[3].r || hand[3].r == hand[4].r) return 2; // Brutally bad luck, High card return 1; } void sorthand(cards hand[5]) { int i, j; cards v; // Selection sort for(i = 0; i < 4; i++) { int min = i; for(j = i + 1; j < 5; j++) if(hand[j].r < hand[min].r) min = j; v = hand[i]; hand[i] = hand[min]; hand[min] = v; } } // @END_OF_SOURCE_CODE