HDOJ-1528 Card Game Cheater (贪心,田忌赛马)

版权声明:个人学习笔记记录 https://blog.csdn.net/Ratina/article/details/83715683

题目:HDOJ-1528

题目描述:
Adam和Eve玩一副扑克牌,两边分到相同数量的牌,摆好以后对应位置的牌比大小,每个对应位置,谁的牌大谁得一分。Adam牌已经摆好位置,Eve已经知道Adam所有位置的牌是什么,求Eve能得到的最多分数。
牌的大小2<3<4<…<9<T<J<Q<K<A,当牌大小相同时,花色C<D<S<H。

思路:
将牌面统一转化成对应大小顺序的数字进行存储,牌的大小2,3,4,…,K,A对应20,30,40,…,130,140,牌的花色C,D,S,H对应1,2,3,4,然后两者相加。
同样的两边按顺序排好,依次比较,这里比田忌赛马简单的就是,不会存在相同的牌,每一对牌一定有胜负而不存在平局。

以下AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int x[],int i)  //读取操作
{
	char v, s;     //v为大小,s为花色
	v = getchar();
	s = getchar();
	if ('2' <= v && v <= '9')
		x[i] = (v - '0') * 10;
	else if (v == 'T')
		x[i] = 10 * 10;
	else if (v == 'J')
		x[i] = 11 * 10;
	else if (v == 'Q')
		x[i] = 12 * 10;
	else if (v == 'K')
		x[i] = 13 * 10;
	else if (v == 'A')
		x[i] = 14 * 10;
	if (s == 'C')
		x[i] += 1;
	else if (s == 'D')
		x[i] += 2;
	else if (s == 'S')
		x[i] += 3;
	else if (s == 'H')
		x[i] += 4;
}
int main()
{
	int a[27], e[27];
	int pa, pes,pee;
	int N, k, i,ans;
	scanf("%d", &N);
	while (N--)
	{
		scanf("%d", &k);
		ans = 0;
		for (i = 0; i < k; i++)
		{
			getchar();
			read(a, i);
		}
		for (i = 0; i < k; i++)
		{
			getchar();
			read(e, i);
		}
		sort(a, a + k);
		sort(e, e + k);    //均从小到大排序
		pa = k - 1;    //pa初始指向Adam牌末尾(最大牌)
		pes = 0;       //pes指向Eve牌开头
		pee = k - 1;    //pee指向Eve牌末尾
		while (1)      //不用考虑相同牌的田忌赛马
		{
			if (e[pee] > a[pa])
			{
				pee--;
				pa--;
				ans++;
			}
			else
			{
				if (e[pes] > a[pa])
					ans++;
				pes++;
				pa--;
			}
			if (pa < 0)
				break;
		}
		printf("%d\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ratina/article/details/83715683