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;
	
	//straight-flush   同花顺
	if (checkStraight(tmp) && checkFlush(tmp))
		return 0;
	

	//four-of-a-kind   炸弹
	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;
	}

	//full-house      满堂红 三张同点牌加上一对 
	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;

	//flush	      同花
	if (checkFlush(tmp))
		return 3;

	//straight	  顺子(注意A即可接2也可以接K)
	if (checkStraight(tmp))
		return 4;

	//three-of-a-kind   三张相同的牌 
	for (i = 0; i < 3; ++i)
	{
		if (tmp[i].v == tmp[i + 1].v && tmp[i + 1].v == tmp[i + 2].v)
			return 5;
	}

	//two-pairs	  两对对子
	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;

	//one-pair	  一对对子 
	for (i = 0; i < 4; ++i)
	{
		if (tmp[i].v == tmp[i + 1].v)
			return 7;
	}

	return 8;
}

int main()
{
//	freopen("e:\\input.txt", "r", stdin);

	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: %s\n", 
			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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值