題目大意:手上有5張牌, 堆上有5張牌, 現在要求捨棄手上的n(0<=n<=5)牌, 從堆的面上拿n張牌, 假設了你有特異功能, 可以知道堆上的5張牌從上到下是是什麼, 要求最後得到的牌要面值最大, 一張牌由數字與花色組成。
思路:就是一一枚舉出每種請況, 然後判斷(不懂打牌的孩子這道題傷不起啊)
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char str[3];
}e[12], ee[12], ans[12];
int p = 0, max = 0;
char str[][19] = {"highest-card","one-pair","two-pairs","three-of-a-kind",
"straight","flush","full-house","four-of-a-kind","straight-flush"};
char str1[19] = "123456789:;<=", str2[10] = "CDHS";
int cmp(void const *a, void const *b)
{
return (*(struct node *)a).str[0]-(*(struct node *)b).str[0];
}
void change(int p)
{
if(ee[p].str[0] == 'A') {ee[p].str[0] = '1'; return ;}
if(ee[p].str[0] == 'T') {ee[p].str[0] = ':'; return ;}
if(ee[p].str[0] == 'J') {ee[p].str[0] = ';'; return ;}
if(ee[p].str[0] == 'Q') {ee[p].str[0] = '<'; return ;}
if(ee[p].str[0] == 'K') {ee[p].str[0] = '='; return ;}
return ;
}
int judge()
{
int i = 0, p = 0, num1[5], num2[5];
char ans1[10], ans2[10];
qsort(ans, 5, sizeof(ans[0]), cmp);//ans中的字符可以看成1到13的數字
for(i = 0; i<5; i++)
{
num1[i] = num2[i] = 1;
}
for(i = 0; i<5; i++)
{
ans1[i] = ans[i].str[0];//取面值
ans2[i] = ans[i].str[1];//取花色
if(i)
{
if(ans1[i] == ans1[i-1])//記錄出 現次數
num1[i] += num1[i-1];
if(ans2[i] == ans2[i-1])
num2[i] += num2[i-1];
}
}
p = ans1[0]-'1';
if(p<10 && strncmp(ans1, str1+p, 5) == 0 && num2[4] == 5)
return 8;
if(ans1[0] == '1' && ans1[1] == ':' && ans1[2] == ';' && ans1[3] == '<' && ans1[4] == '=' && num2[4] == 5)
return 8;
if(num1[3] == 4 || num1[4] == 4)
return 7;
if((num1[2] == 3 && num1[4] == 2) || (num1[1] == 2 && num1[4] == 3))
return 6;
if(num2[4] == 5)
return 5;
if(p<10 && strncmp(ans1, str1+p, 5) == 0)
return 4;
if(ans1[0] == '1' && ans1[1] == ':' && ans1[2] == ';' && ans1[3] == '<' && ans1[4] == '=')
return 4;
if((num1[4] == 3 && num1[0] == 1 && num1[1] == 1) || (num1[3] == 3 && num1[0] == 1 && num1[4] == 1) || (num1[2] == 3 && num1[4] == 1))
return 3;
if((num1[1] == 2 && num1[3] == 2 && num1[4] == 1) || (num1[2] == 2 && num1[4] == 2) || (num1[1] == 2 && num1[4] == 2))
return 2;
if((num1[2] == 2) || (num1[3] == 2) || (num1[4] == 2) || (num1[1] == 2))
return 1;
return 0;
}
void create(int cu, int p)//枚舉每種情況
{
int i = 0, j = 0, k = 0;
if(cu == 6)
{
for(j = p, i = 5; i<=9-p; i++)
{
strcpy(ee[j++].str, e[i].str);
change(j-1);
}
for(i = 0; i<5; i++)
strcpy(ans[i].str, ee[i].str);
k = judge();
max = max>k? max:k;
return ;
}
for(i = 0; i<2; i++)
{
if(i)
{
strcpy(ee[p].str, e[cu-1].str);
change(p);
create(cu+1, p+1);
}
else
create(cu+1, p);
}
}
int main()
{
int i = 0;
while(scanf("%s",e[0].str) != EOF)
{
printf("Hand: ");
printf("%s ",e[0].str);
for(i = 1; i<10; i++)
{
scanf("%s",e[i].str);
if(i == 5)
printf("Deck: ");
printf("%s ",e[i].str);
}
printf("Best hand: ");
max = p = 0;
create(1, p);
printf("%s\n", str[max]);
} www.2cto.com
return 0;
}