题目的意思就是打德州扑克啦。
每行10张牌,前5张是你手牌,后5张是你桌上的牌,你可以选择任意几张丢到,并按顺序从桌上补全。问最大是什么。
大小依次是 同花顺,四条,三条带一对,同花,顺子,三条加两个单张,两对,一对,高牌;
做法就是去枚举手上5张牌的全部子集,不足5张的用桌上的牌补满,然后看看每个子集是什么牌。当然直接去看难度很大,所以要先排序。
我按花色排了一个序,也按点数排了一个序(不过后来发现花色排序不需要用到)。
我判断顺子用的方法是把排好序的string中点数拿出来另外组个,然后来看看这个字符串和我事先写好的所有顺子的情况会不会符合。
AC代码:
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
string hand[5];
string deck[5];
string besthand[5];
string besthand2[5];
string str = "23456789T 2345A 789JT 89JQT 9JKQT AJKQT ";
string temp;
string res[10] = {"" ,"highest-card", "one-pair" , "two-pairs" , "three-of-a-kind" , "straight" , "flush" , "full-house" , "four-of-a-kind" , "straight-flush"};
int cmp (const void *a ,const void *b) {
return *((string*)a) > *((string*)b);
}
int num_of_one (int num) {
int count = 0 ;
for (int i = 0 ; i < 5 ; i++ ) {
if (num & 1 << i)
count++;
}
return count;
}
int judge () {
qsort(besthand , 5 , sizeof(besthand[0]) , cmp);
qsort(besthand2 , 5 , sizeof(besthand2[0]) , cmp);
int is = true;
for (int i = 0 ; i < 4 ; i++) {
if (besthand[i][0] != besthand[i + 1][0]) {
is = false;
break;
}
}
temp = "";
if (is == true) {
for (int i = 0 ; i < 5 ; i++) {
temp += besthand[i][1];
}
if(str.find(temp) == string::npos )
is = false;
}
if (is == true) {
return 9;
}
if (besthand2[0][0] == besthand2[1][0])
if (besthand2[1][0] == besthand2[2][0])
if (besthand2[2][0] == besthand2[3][0])
is = true;
if (besthand2[1][0] == besthand2[2][0])
if (besthand2[3][0] == besthand2[4][0])
if (besthand2[2][0] == besthand2[3][0])
is = true;
if (is == true)
return 8;
if (besthand2[0][0] == besthand2[1][0])
if (besthand2[1][0] == besthand2[2][0])
if (besthand2[3][0] == besthand2[4][0])
is = true;
if (besthand2[0][0] == besthand2[1][0])
if (besthand2[2][0] == besthand2[3][0])
if (besthand2[3][0] == besthand2[4][0])
is = true;
if (is == true)
return 7;
is = true;
for (int i = 0 ; i < 4 ; i++) {
if (besthand[i][0] != besthand[i + 1][0]) {
is = false;
break;
}
}
if (is == true)
return 6;
is = true;
temp = "";
for (int i = 0 ; i < 5 ; i++) {
temp += besthand2[i][0];
}
if(str.find(temp) == string::npos )
is = false;
if (is == true)
return 5;
if (besthand2[0][0] == besthand2[1][0])
if (besthand2[1][0] == besthand2[2][0])
if (besthand2[3][0] != besthand2[4][0])
is = true;
if (besthand2[0][0] != besthand2[1][0])
if (besthand2[3][0] == besthand2[4][0])
if (besthand2[2][0] == besthand2[3][0])
is = true;
if (besthand2[0][0] != besthand2[4][0])
if (besthand2[1][0] == besthand2[2][0])
if (besthand2[2][0] == besthand2[3][0])
is = true;
if (is == true)
return 4;
int count = 0;
for (int i = 0 ; i < 4 ; i++) {
if (besthand2[i][0] == besthand2[i + 1][0])
count++;
}
if (count == 2)
return 3;
if (count == 1)
return 2;
if (count == 0)
return 1;
}
int get_besthand(int count ,int num ) {
int k = 0;
for (int i = 0 ; i < 5 ; i++) {
if (num & 1 << i) {
besthand[k++] += hand[i][1] ;
besthand[k - 1] += hand[i][0];
besthand2[k - 1] = hand[i];
}
}
for (int i = 0 ; k < 5 ; i++) {
besthand[k++] += deck[i][1];
besthand[k - 1] += deck[i][0];
besthand2[k - 1] = deck[i];
}
return judge();
}
int main () {
while (cin >> hand[0]) {
for (int i = 1 ; i < 5 ; i++) {
cin >> hand[i];
}
for (int i = 0; i < 5 ; i++) {
cin >> deck[i];
}
int m = 0;
for (int i = 0 ; i < (1 << 5) ; i++) {
int count = num_of_one(i);
int level = get_besthand(count ,i);
for (int i = 0 ; i < 5 ; i++) {
besthand[i] = "";
besthand2[i] = "";
}
if (level > m) {
m = level;
if (level == 9)
break;
}
}
cout <<"Hand: ";
for (int i = 0 ; i < 5 ; i++)
cout << hand[i] << " ";
cout << "Deck: ";
for (int i = 0 ; i < 5 ; i++ )
cout << deck[i] << " ";
cout << "Best hand: ";
cout << res[m] << endl;
}
return 0 ;
}