题目:131 - The Psychic Poker Player
题目大意:刚开始我一点都不懂题目要我们做什么,后来看了别人的题解,发现原来是打某种类型的纸牌,手上有5张牌,桌上也五张牌,这个人可艺从手中丢弃某些牌,从轴上摸去相等数量的牌,桌上的牌是有顺序的,然后问手上最好的牌是什么。
解题思路:直接枚举丢牌和摸牌的情况,一共有2的5次方种,这里我用了位运算,用一个数来代替舍弃牌的情况,(这个数的二进制数某个位置上有1,代表舍弃这个位置上的数),将这个数与1,10,100,1000,10000相与,就可以知道哪个位置上有1了,这样就可以知道舍弃哪个位置上的牌。剩下的就是得判断手上的是什么牌,细心就可以过得,题目给的代码能过一般就没问题了。
注意:每次牌都还得回到原来初始的状态。还有里面有字母TK什么的需要处理成数字比较方便。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N = 10; const char ans[N][2*N] = {"straight-flush", "four-of-a-kind", "full-house", "flush", "straight", "three-of-a-kind", "two-pairs", "one-pair", "highest-card"}; int choice; int str[5], color[5]; char s[10][4]; int is_kind() { int num[15], i; memset(num, 0, sizeof(num)); for(i = 0; i < 5; i++) num[str[i]]++; sort(num, num + 15); int count = 0; for(i = 14; i >= 0; i--) { if(num[i] == 4) return 1; else if(num[i] == 3) { for(int j = i - 1; j >= 0; j--) if(num[j] == 2) return 2; return 5; } else if(num[i] == 2) count++; } if(count == 2) return 6; else if(count == 1) return 7; else return -1; } void manage() { int i; for(i = 0; i < 5; i++) { if(s[i][0] == 'A') str[i] = 1; else if(s[i][0] == 'T') str[i] = 10; else if(s[i][0] == 'J') str[i] = 11; else if(s[i][0] == 'Q') str[i] = 12; else if(s[i][0] == 'K') str[i] = 13; else str[i] = s[i][0] - '0'; } for(i = 0; i < 5; i++) { if(s[i][1] == 'C') color[i] = 1; else if(s[i][1] == 'D') color[i] = 2; else if(s[i][1] == 'H') color[i] = 3; else color[i] = 4; } } int is_Flush() { int count = 0; for(int i = 1; i < 5; i++) if(color[i] == color[0]) count++; if(count == 4) return 1; return 0; } int is_Straight() { int count = 0, i; int s1[5]; for(i = 0; i < 5; i++) s1[i] = str[i]; sort(s1, s1 + 5); for(i = 1; i < 5; i++){ if(s1[i] == s1[i - 1] + 1 ) count++; } if(count == 4) return 1; count = 0; if(s1[0] == 1 && s1[1] == 10) { for(i = 2; i < 5; i++) if(s1[i] == s1[i - 1] + 1) count++; if(count == 3) return 1; } return 0; } int find() { int c = is_kind(); if( c == -1) { if(is_Straight() ) { if(is_Flush()) c = 0; else c = 4; } else { if(is_Flush()) c = 3; else c = 8; } } else { if(is_Flush() && c > 3) c = 3; } return c; } int main() { while(1) { int i, j; for(i = 0; i < 10; i++) { if(!i) { if(scanf("%s", s[0]) != 1) break; } else scanf("%s", s[i]); } if(i == 0) break; choice = 10; for(i = 0; i < (1 << 5); i++) { manage(); int n = 0, d[5]; memset(d, 0, sizeof(d)); for(j = 0; j < 5; j++) if(i & (1 << j)) { a n++; d[j]++; } int k = 5, v; for(v = 0; v < 5; v++) if(d[v]) { if(s[k][0] == 'A') str[v] = 1; else if(s[k][0] == 'T') str[v] = 10; else if(s[k][0] == 'J') str[v] = 11; else if(s[k][0] == 'Q') str[v] = 12; else if(s[k][0] == 'K') str[v] = 13; else str[v] = s[k][0] - '0'; if(s[k][1] == 'C') color[v] = 1; else if(s[k][1] == 'D') color[v] = 2; else if(s[k][1] == 'H') color[v] = 3; else color[v] = 4; k++; } int m = find(); if(m < choice) choice = m; } printf("Hand: "); for(i = 0; i < 5; i++) printf("%s ", s[i]); printf("Deck: "); for(i = 5; i < 10; i++) printf("%s ", s[i]); printf("Best hand: %s\n", ans[choice]); } return 0; }