首页 > 分享 > uva 131 有超能力的纸牌玩家

uva 131 有超能力的纸牌玩家

题目:手里有五张牌,桌上有一堆牌(五张),你可以弃掉手中的k张牌,然后从牌堆中取最上面的k个。

            比较规则如下:(按优先级排序)

            1.straight-flush:同花顺,牌面为T(10) - A,这里不论花色是否相同;

            2.four-of-a-kind:四条,牌面有4个相同的值;

            3.full-house:船牌,牌面有3个相同值,剩下2个也相同值;

            4.flush:同花,五张牌的花色相同,不是同花顺;

            5.straight:顺子,五张牌的值连续,A可以作为1也可以作为14;

            6.three-of-a-kind:三条,牌面有3个相同的值;

            7.two-pairs:两对,牌面有2个对子;

            8.one-pair:一对,牌面有一个对子,即2个同值;

            9.highest-card:大牌,没有以上牌型。

#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <iostream>

using namespace std;

struct card

{

int v;

char t;

};

card hand[5], table[5];

char trans1[20];

char trans2[10][20] = {"straight-flush", "four-of-a-kind", "full-house", "flush", "straight", "three-of-a-kind",

"two-pairs", "one-pair", "highest-card"};

int cmp(const void *a, const void *b)

{

card *c1 = (card *)a;

card *c2 = (card *)b;

return c1->v - c2->v;

}

bool checkStraight(card tmp[])

{

int i;

for (i = 1; i < 5; ++i)

{

if (tmp[i].v - tmp[i - 1].v != 1)

break;

}

if (i >= 5)

return true;

if (tmp[0].v == 1)

{

if (tmp[1].v == 10 && tmp[2].v == 11 && tmp[3].v == 12 && tmp[4].v == 13)

return true;

}

return false;

}

bool checkFlush(card tmp[])

{

int i;

for (i = 1; i < 5; ++i)

{

if (tmp[i].t != tmp[i - 1].t)

break;

}

if (i >= 5)

return true;

else

return false;

}

int check(card tmp[])

{

qsort(tmp, 5, sizeof(tmp[0]), cmp);

int i;

if (checkStraight(tmp) && checkFlush(tmp))

return 0;

for (i = 0; i < 2; ++i)

{

if (tmp[i].v == tmp[i + 1].v && tmp[i + 1].v == tmp[i + 2].v && tmp[i + 2].v == tmp[i + 3].v)

return 1;

}

if (tmp[0].v == tmp[1].v && tmp[2].v == tmp[3].v && tmp[3].v == tmp[4].v)

return 2;

if (tmp[0].v == tmp[1].v && tmp[1].v == tmp[2].v && tmp[3].v == tmp[4].v)

return 2;

if (checkFlush(tmp))

return 3;

if (checkStraight(tmp))

return 4;

for (i = 0; i < 3; ++i)

{

if (tmp[i].v == tmp[i + 1].v && tmp[i + 1].v == tmp[i + 2].v)

return 5;

}

if (tmp[1].v == tmp[2].v && tmp[3].v == tmp[4].v)

return 6;

if (tmp[0].v == tmp[1].v && tmp[3].v == tmp[4].v)

return 6;

if (tmp[0].v == tmp[1].v && tmp[2].v == tmp[3].v)

return 6;

for (i = 0; i < 4; ++i)

{

if (tmp[i].v == tmp[i + 1].v)

return 7;

}

return 8;

}

int main()

{

int i;

trans1[1] = 'A';

trans1[10] = 'T';

trans1[11] = 'J';

trans1[12] = 'Q';

trans1[13] = 'K';

for (i = 2; i <= 9; ++i)

trans1[i] = i + '0';

char s[5];

while (scanf("%s", s) == 1)

{

i = 0;

if (s[0] == 'T')

hand[i].v = 10;

else if (s[0] == 'J')

hand[i].v = 11;

else if (s[0] == 'Q')

hand[i].v = 12;

else if (s[0] == 'K')

hand[i].v = 13;

else if (s[0] == 'A')

hand[i].v = 1;

else

hand[i].v = s[0] - '0';

hand[i].t = s[1];

while (++i < 5)

{

scanf("%s", s);

if (s[0] == 'T')

hand[i].v = 10;

else if (s[0] == 'J')

hand[i].v = 11;

else if (s[0] == 'Q')

hand[i].v = 12;

else if (s[0] == 'K')

hand[i].v = 13;

else if (s[0] == 'A')

hand[i].v = 1;

else

hand[i].v = s[0] - '0';

hand[i].t = s[1];

}

for (i = 0; i < 5; ++i)

{

scanf("%s", s);

if (s[0] == 'T')

table[i].v = 10;

else if (s[0] == 'J')

table[i].v = 11;

else if (s[0] == 'Q')

table[i].v = 12;

else if (s[0] == 'K')

table[i].v = 13;

else if (s[0] == 'A')

table[i].v = 1;

else

table[i].v = s[0] - '0';

table[i].t = s[1];

}

int cxCard, ans = 10;

for (cxCard = 0; cxCard <= 5; ++cxCard)

{

card l_hand[5];

for (i = 0; i < (1 << 5); ++i)

{

int cn = 0;

int tmp = i;

while (tmp)

{

if (tmp & 1)

++cn;

tmp >>= 1;

}

if (cn != cxCard)

continue;

memcpy(l_hand, hand, 5 * sizeof(hand[0]));

tmp = i;

int pos_h = 0, pos_t = 0;

while (tmp)

{

if (tmp & 1)

{

l_hand[pos_h] = table[pos_t++];

}

tmp >>= 1;

++pos_h;

}

int val = check(l_hand);

ans = ans < val ? ans : val;

}

}

printf("Hand: %c%c %c%c %c%c %c%c %c%c Deck: %c%c %c%c %c%c %c%c %c%c Best hand: %sn",

trans1[hand[0].v], hand[0].t, trans1[hand[1].v], hand[1].t, trans1[hand[2].v], hand[2].t,

trans1[hand[3].v], hand[3].t, trans1[hand[4].v], hand[4].t, trans1[table[0].v], table[0].t,

trans1[table[1].v], table[1].t, trans1[table[2].v], table[2].t, trans1[table[3].v], table[3].t,

trans1[table[4].v], table[4].t, trans2[ans]);

}

return 0;

}

相关知识

我发现了最适合春节花切的纸牌!
世界上真的有人有超能力吗?
蜘蛛纸牌在线
[编程题] 纸牌游戏
王者荣耀:花木兰要展示超能力,宠妻狂魔兰陵王只好配合演出!
朝鲜族“花图”游戏
中海地产 – 母亲节,爱你就是超能力——广州中海会母亲节活动温馨开展
科学家利用花的“超能力”为新药铺平道路
花切小介绍|一起来炫酷一夏
玩家请上车

网址: uva 131 有超能力的纸牌玩家 https://m.huajiangbk.com/newsview761913.html

所属分类:花卉
上一篇: 《南方公园:完整破碎》角色创建肤
下一篇: 第532章 楚氏花仙子基地