ZOJ1111:Poker Hands

A poker deck contains 52 cards - each card has a suit which is one of clubs, diamonds, hearts, or spades (denoted C, D, H, S in the input data). Each card also has a value which is one of 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace (denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A). For scoring purposes, the suits are unordered while the values are ordered as given above, with 2 being the lowest and ace the highest value.

A poker hand consists of 5 cards dealt from the deck. Poker hands are ranked by the following partial order from lowest to highest

  • High Card. Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on.
  • Pair. 2 of the 5 cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order.
  • Two Pairs. The hand contains 2 different pairs. Hands which both contain 2 pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card.
  • Three of a Kind. Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the 3 cards.
  • Straight. Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card.
  • Flush. Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.
  • Full House. 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.
  • Four of a kind. 4 cards with the same value. Ranked by the value of the 4 cards.
  • Straight flush. 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand.
Your job is to compare several pairs of poker hands and to indicate which, if either, has a higher rank.

Input Specification

Several lines, each containing the designation of 10 cards: the first 5 cards are the hand for the player named "Black" and the next 5 cards are the hand for the player named "White."

Output Specification

For each line of input, print a line containing one of:

   Black wins.
   White wins.
   Tie.

Sample Input

2H 3D 5S 9C KD 2C 3H 4S 8C AH
2H 4S 4C 2D 4H 2S 8S AS QS 3S
2H 3D 5S 9C KD 2C 3H 4S 8C KH
2H 3D 5S 9C KD 2D 3H 5C 9S KH

Sample Output

White wins.
Black wins.
Black wins.

Tie.

两个人玩牌,每个人五张扑克牌,2最小,A最大。规则如下:

  • High Card. Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on. 
    • 最低等级,降序排序,然后按照字典序比较。
  • Pair. 2 of the 5 cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order. 
    • 一对,两个牌的数字相同,先按对子的值比,如果一样,就把其余的三个降序排序,字典序比较。
  • Two Pairs. The hand contains 2 different pairs. Hands which both contain 2 pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card. 
    • 两个一对,按最大的那个对子比,如果一样就按第二大的,还是一样,就按剩下的那张牌比较。
  • Three of a Kind. Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the 3 cards. 
    • 三角, 三个牌的数字相同,就按这三个数字的值比。
  • Straight. Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card. 
    • 顺子, 五个牌的数字连续。按最大的那个牌比。
  • Flush. Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card. 
    • 同花, 五个牌的花色都一样,按照最低等级的规则比较
  • Full House. 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards. 
    • 马, 由顺子和一对组成,按直线比。
  • Four of a kind. 4 cards with the same value. Ranked by the value of the 4 cards. 
    • 四角,四个牌的数字一样,按这四个数字比。
  • Straight flush. 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand. 

    • 同花顺, 五张牌数字连续且花色相同。按最大的那个牌比较。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
int b[10];

struct Poke{
	int n;
	char m;
}blackpoker[10],whitepoker[10];



int changeValue(char x){
	switch(x){
		case 'T':return 10 ;break;
		case 'J':return 11 ;break;
		case 'Q':return 12 ;break;
		case 'K':return 13 ;break;
		case 'A':return 14 ;break;
		default:return x-'0';break;
	}
}

int judgex(Poke *bow){
	int temp1=0;
	int temp2=0;
	int k=0,k1=0;
	if(bow[0].n+1==bow[1].n&&bow[1].n+1==bow[2].n&&bow[2].n+1==bow[3].n&&bow[3].n+1==bow[4].n)
		temp1=1;
	if(bow[0].m==bow[1].m&&bow[1].m==bow[2].m&&bow[2].m==bow[3].m&&bow[3].m==bow[4].m)
		temp2=1;
	if(temp1==1&&temp2==1)
		return 9;					//Straight Flush
	else if(temp1!=1&&temp2==1)
		return 6;					//Flush
	else if(temp1==1&&temp2!=1)
		return 5;					//Straight:
	else
	{
		for(int i=1;i<=4;i++)
			if(bow[i].n!=bow[i-1].n)
				b[k1++]=i;
		//printf("%d\n",k1);
		if(k1==0)					 	//*********->注意fuck的只判断了n,还有m_cao所以2H 2H 2H 2H 2D此为8类 
			return 8;
		if(k1==1)
		{
			if(b[0]==2||b[0]==3)
				return 7;	//Full House
			else if(b[0]==1||b[0]==4)
				return 8;	//Four of a Kind:
		}
		if(k1==2)
		{
			if(b[0]==1&&b[1]==2||b[0]==1&&b[1]==4||b[0]==3&&b[1]==4)
				return 4;
			else
				return 3;
		}
		if(k1==3)
		{
			return 2;
		}
		if(k1==4)
		return 1;
	}
		
}


int cmp(Poke a,Poke b)  
{  
    return a.n<b.n;  
}  
 
int findpair(Poke *ax)  
{  
    int i;  
    for(i = 0; i<=4; i++)  
    {  
        if(i==0&&ax[i].n == ax[i+1].n)  
            return 1;  
        else if(ax[i].n == ax[i-1].n)  
            return i;  
    }
}  
 
 int findone(Poke *ax)  
{  
    int i;  
    for(i = 0; i<=4; i++)  
    {  
        if(i == 0 && ax[i].n!=ax[i+1].n)  
            return 0;  
        if(i == 4 && ax[i].n!=ax[i-1].n)  
            return 4;  
        if(ax[i].n!=ax[i-1].n && ax[i].n!=ax[i+1].n)  
            return i;  
    } 
	return -1;   
}  
  
  
int pare(int x,Poke *blackpoker,Poke *whitepoker)
{
	
	if(x==9||x==5)
		{
			if(blackpoker[4].n>whitepoker[4].n)
				return 1;
			else if(blackpoker[4].n<whitepoker[4].n)
				return -1;
			return 0;
		}
	else if(x==8||x==7||x==4)
	{
		if(blackpoker[2].n>whitepoker[2].n)
			return 1;
		else
			return -1;
		return 0;
	}
	else if(x==6)
	{
		for(int i=4;i>=0;i--)
		{
			if(blackpoker[i].n>whitepoker[i].n)
			{
				return 1;
				}	
			else if(blackpoker[i].n<whitepoker[i].n)
				{
					return -1;	
				}
		}
		return 0;
	}
	else if(x==3)
	{
		if(blackpoker[3].n>whitepoker[3].n)
			return 1;
		else if(blackpoker[3].n<whitepoker[3].n)
			return -1;
		else
		{
			if(blackpoker[1].n>whitepoker[1].n)
				return 1;
			else if(blackpoker[1].n<whitepoker[1].n)
				return -1;
			int a=findone(blackpoker);
			int b=findone(whitepoker);
			if(blackpoker[a].n>whitepoker[b].n)  
                return 1;  
            else if(blackpoker[a].n<whitepoker[b].n)  
                return -1;  
			return 0;
		}	
	}
	else if(x==2)
	{
		int temp1=findpair(blackpoker);
		int temp2=findpair(whitepoker);
		int bs[10],ws[10];
		int k1=0,k2=0;
		if(blackpoker[temp1].n>whitepoker[temp2].n)
			return 1;
		else if(blackpoker[temp1].n<whitepoker[temp2].n)
			return -1;
			
				for(int i=0;i<=4;i++)
				{
					if(i!=temp1&&i!=temp1-1)
						bs[k1++]=blackpoker[i].n;
					if(i!=temp2&&i!=temp2-1)
						ws[k2++]=whitepoker[i].n;
				}
			
		for(int i=2;i>=0;i--)
		{
			if(bs[i]>ws[i])
				return 1;
			else if(bs[i]<ws[i])
				return -1;
		}
		return 0;
		
	}
	
	else if(x==1)
	{
		for(int i=4;i>=0;i--)
		{
			if(blackpoker[i].n>whitepoker[i].n)
				return 1;
			else if(blackpoker[i].n<whitepoker[i].n)
				return -1;
		}
		return 0;
	 } 
	return 0; 
}


int main()
{
	int x,y;
	char a[50];
	 while(gets(a))
	 {
	 	Poke whitepoker[10],blackpoker[10];
        int index=0;
        for(int i=0;i<15;i+=3){
            blackpoker[index].n=changeValue(a[i]);
            blackpoker[index].m=a[i+1];

            whitepoker[index].n=changeValue(a[i+15]);
            whitepoker[index].m=a[i+16];
            index++;

        }
        
	   sort(whitepoker,whitepoker+5,cmp);
       sort(blackpoker,blackpoker+5,cmp);
       x=judgex(blackpoker);
       y=judgex(whitepoker);
       //printf("%d %d \n",x,y);
       if(x>y)
	   	printf("Black wins.\n");
	   else if(x<y)
	   	printf("White wins.\n");
       else
       {
       		int judge=pare(x,blackpoker,whitepoker);
       		if(judge==1)
       			printf("Black wins.\n");
       		else if(judge==-1)
       			printf("White wins.\n");
       		else 
       			printf("Tie.\n");
	   }
	    
	   
	}
return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值