uva131 Time limit exceeded

第一次提交超时(Time limit exceeded )了,想了半天不知道哪里能再简化。原来是主函数中写了while(1)循环,怪不得会超时,要写成while(cin>>ch)就好了。

这个题不难,考察细致程度,当然要读懂题目,开始不明白输出那是些什么东西,查了度娘后明白了,现在把扑克规则粘贴过来供大家参考。

下面列出从大到小的扑克牌面. 这是所有的扑克通用规则。

1
Royal Flush 同花大顺又称皇家同花顺 它是所有德州扑克中的王牌,即使您经常玩扑克,也很少见到这样的牌。好比打高尔夫球一杆进洞一样。它是由T(10)到Ace的清一色同花组成。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
2
Straight Flush 同花顺 除了由最大同花所组成的同花大顺以外的同花组成的顺子。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
3
Four-of-a-Kind 四条 四张同样的牌+任意一张牌 。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
4

Full House 俘虏或船牌或葫芦 三条带一对,即三张同样的牌带两张同样的牌。如都是Full House,则先比较谁的三条大,如三条一样大,则比谁的两对大。如:

Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
5
Flush 五张同花 用五张同一花色但不相连的牌型组成,如都是五张同花,则谁的同花牌大谁赢。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
6
Straight 五张顺子 由五张相连但不同花色的牌组成,在连牌中,Ace是既可作最大也可以作最小的牌。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
7
Three-of-a-Kind 三条 即三张同样的牌。它有两种叫法,取决于一对牌是在您手中还是在桌上。一对在手中,桌上有一张,称之为“set”;v如手中有一张,桌上有一对,则称之为“Three of A Kind”。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
8
Two Pair 两对 由五张牌中的两对牌组成。如果都有两对,则先比大对,再比小对 。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
9
One Pair 一对 当不止一人有同样的一对牌时,则要比一对后面的牌,称之为“Kickers”。记住,德州扑克是挑选最好的五张牌去比。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
  
10
High Card 大牌 无以上任何牌型时,决定牌的大小 。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
进行判断的时候就是判断三个指标,是否同花(flush),是否是顺子(straight),是否含对子。其中对子又分为一个3对+一个2对、一个3对、两个2对、一个2对。

判断完后根据从大到小的顺序依次判断牌型符合哪种要求输出即可。

哦对了,还是用到了位向量法生成子集。

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

struct card
{
	char value,suit;
};

card input[10];
char besthand[9][20]={"straight-flush","four-of-a-kind","full-house", "flush","straight","three-of-a-kind","two-pairs","one-pair","highest-card"};
int Min;

int cmp(const void* a,const void* b)
{
	return *(int*)a-*(int*)b;
}
int cmp2(const void* a,const void* b)
{
	return *(int*)b-*(int*)a;
}
int getpairs(int cd[])
{
	int pile[14]={0};
	for (int i=0;i<5;i++)
		pile[cd[i]]++;
	qsort(pile,14,sizeof(pile[0]),cmp2);
	if (pile[0]==4) return 5;
	else if (pile[0]==3&&pile[1]==2) return 4;
	else if (pile[0]==3) return 3;
	else if (pile[0]==2&&pile[1]==2) return 2;
	else if (pile[0]==2) return 1;
	else return 0;
}
int classify(card cd[])
{
	bool flush=1,strat=1;
	char suit=cd[0].suit;
	int cdv[5],pairs;
	for (int i=0;i<5;i++)
	{
		if (cd[i].value=='A') cdv[i]=1;
		else if (cd[i].value=='T') cdv[i]=10;
		else if (cd[i].value=='J') cdv[i]=11;
		else if (cd[i].value=='Q') cdv[i]=12;
		else if (cd[i].value=='K') cdv[i]=13;
		else cdv[i]=cd[i].value-'0';
		if (suit!=cd[i].suit) 
			flush=0;
	}
	qsort(cdv,5,sizeof(cdv[0]),cmp);
	if (cdv[0]>1&&cdv[0]<10)
	{
		for (int i=1;i<5;i++)
		{
			if (cdv[i]-cdv[0]!=i)
			{
				strat=0;
				break;
			}
		}
	}
	else if (cdv[0]==1)
	{
		if (cdv[1]==2&&cdv[2]==3&&cdv[3]==4&&cdv[4]==5)
			strat=1;
		else if (cdv[1]==10&&cdv[2]==11&&cdv[3]==12&&cdv[4]==13)
			strat=1;
		else strat=0;
	}
	else strat=0;
	pairs=getpairs(cdv);
	if (flush&&strat) return 0;
	else if (pairs==5) return 1;
	else if (pairs==4) return 2;
	else if (flush) return 3;
	else if (strat) return 4;
	else if (pairs==3) return 5;
	else if (pairs==2) return 6;
	else if (pairs==1) return 7;
	else return 8;
}
void subset(int n,int B[],int cur)
{
	if (cur==n)
	{
		int dex=0;
		card cd[5];
		for (int i=0;i<cur;i++)
		{
			if (B[i])
				cd[dex++]=input[i];
		}
		for (int j=dex,k=5;j<5;j++)
		{
			cd[j]=input[k++];
		}
		int tmp=classify(cd);
		if (tmp<Min) Min=tmp;
		return ;
	}
	B[cur]=1;
	subset(n,B,cur+1);
	B[cur]=0;
	subset(n,B,cur+1);
}
void print()
{
	cout<<"Hand: ";
	for (int i=0;i<5;i++)
	{
		cout<<input[i].value<<input[i].suit<<" ";
	}
	cout<<"Deck: ";
	for (int i=5;i<10;i++)
	{
		cout<<input[i].value<<input[i].suit<<" ";
	}
	cout<<"Best hand: "<<besthand[Min]<<endl;
}
int main()
{
	char chv,chs;
	while (cin>>chv>>chs)
	{
		input[0].value=chv;
		input[0].suit=chs;
		getchar();
		for (int i=1;i<10;i++)
		{
			cin>>input[i].value>>input[i].suit;
			getchar();
		}
		int array[5];
		Min=8;
		subset(5,array,0);
		print();

	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值