UVa 10315 - Poker Hands

/* File: 10315.cpp Author: ACboy Date: 2010-4-19 Result: 1A Descripition: UVa 10315 - Poker Hands */ #include <iostream> #include <map> #include <algorithm> using namespace std; struct Card { int value; char type; }; struct Cards { int rank; map<int, int> m; Card cards[5]; char cmp_order[6]; }; Cards black, white; int cmp(const Card & a, const Card & b) { return a.value < b.value; } int card_input(int t) { int value; char type; char input[3] = ""; int i;; for (i = 0; i < 5; i++) { scanf("%s", input); if (strcmp(input, "") == 0) return 0; switch(input[0]) { case 'T': value = 10; break; case 'J': value = 11; break; case 'Q': value = 12; break; case 'K': value = 13; break; case 'A': value = 14; break; default: value = input[0] - '0'; break; } type = input[1]; if (t) { black.cards[i].value = value; black.cards[i].type = type; if (black.m.find(value) != black.m.end()) { black.m[value]++; } else { black.m[value] = 1; } } else { white.cards[i].value = value; white.cards[i].type = type; if (white.m.find(value) != white.m.end()) { white.m[value]++; } else { white.m[value] = 1; } } } return 1; } void get_rand_value(Cards & a) { int size = a.m.size(); int i; int c; int max; int temp; map<int, int>::iterator it; switch (size) { case 2: max = 0; c = 0; for (it = a.m.begin(); it != a.m.end(); it++) { if ((*it).second > max) { max = (*it).second; temp = (*it).first; } } if (max == 4) { a.rank = 8; } else { a.rank = 7; } a.cmp_order[c++] = temp + 'a'; for (i = 4; i >= 0; i--) if (a.cards[i].value != temp) { a.cmp_order[c++] = a.cards[i].value + 'a'; } a.cmp_order[c] = '/0'; break; case 3: a.rank = 3; int b[2]; c = 0; for (it = a.m.begin(); it != a.m.end(); it++) { if ((*it).second == 2) { b[c++] = (*it).first; } } if (b[0] < b[1]) { temp = b[0]; b[0] = b[1]; b[1] = temp; } c = 0; a.cmp_order[c++] = b[0] + 'a'; a.cmp_order[c++] = b[1] + 'a'; for (i = 4; i >= 0; i--) if (a.cards[i].value != b[0] && a.cards[i].value != b[1]) { a.cmp_order[c++] = a.cards[i].value + 'a'; } a.cmp_order[c] = '/0'; break; case 4: max = 0; c = 0; for (it = a.m.begin(); it != a.m.end(); it++) { if ((*it).second > max) { temp = (*it).first; max = (*it).second; } } if (max == 3) { a.rank = 4; } else { a.rank = 2; } a.cmp_order[c++] = temp + 'a'; for (i = 4; i >= 0; i--) if (a.cards[i].value != temp) { a.cmp_order[c++] = a.cards[i].value + 'a'; } a.cmp_order[c] = '/0'; break; case 5: int ok = 1; int ok1 = 1; char type = a.cards[0].type; for (i = 0; i < 5; i++) { if (a.cards[i].type != type) { ok1 = 0; break; } } if (a.cards[4].value == 14) { for (i = 0; i < 4; i++) { if (a.cards[i].value != i + 2) { ok = 0; break; } } if (ok) { if (ok1) { a.rank = 9; } else { a.rank = 5; } } else { if (ok1) { a.rank = 6; } else { a.rank = 1; } } } else { for (i = 0; i < 4; i++) { if (a.cards[i].value + 1 != a.cards[i + 1].value) { ok = 0; break; } } if (ok) { if (ok1) { a.rank = 9; } else { a.rank = 5; } } else { if (ok1) { a.rank = 6; } else { a.rank = 1; } } } c = 0; for(i = 4; i >= 0; i--) { a.cmp_order[c++] = a.cards[i].value + 'a'; } a.cmp_order[c] = '/0'; break; } } void output(int t) { for (int i = 0; i < 5; i++) { if (t) { cout << black.cards[i].value << black.cards[i].type << " "; } else { cout << white.cards[i].value << white.cards[i].type << " "; } } cout << endl; } int main() { #ifndef ONLINE_JUDGE freopen("10315.txt", "r", stdin); #endif while (1) { if (!card_input(1)) break; card_input(0); sort(white.cards, white.cards + 5, cmp); sort(black.cards, black.cards + 5, cmp); get_rand_value(black); get_rand_value(white); if (black.rank > white.rank) { cout << "Black wins." << endl; } else if (black.rank < white.rank) { cout << "White wins." << endl; } else { int temp = strcmp(black.cmp_order, white.cmp_order); if (temp > 0) { cout << "Black wins." << endl; } else if (temp < 0) { cout << "White wins." << endl; } else { cout << "Tie." << endl; } } black.m.clear(); white.m.clear(); } }

你可能感兴趣的:(c,struct,File,iterator,input,output)