Poker Hands

Poker Hands

时间: 1ms        内存:64M

描述:

A poker deck contains 52 cards. Each card has a suit of either clubs, diamonds, hearts, or spades (denoted C, D, H, S in the input data). Each card also has a value of either 2 through 10, jack, queen, king, or ace (denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A). For scoring purposes card values are ordered as above, with 2 having the lowest and ace the highest value. The suit has no impact on value. A poker hand consists of five 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. Two of the five 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 two different pairs. Hands which both contain two 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 three cards. Straight. Hand contains five cards with consecutive values. Hands which both contain a straight are ranked by their highest card. Flush. Hand contains five cards of the same suit. Hands which are both flushes are ranked using the rules for High Card. Full House. Three cards of the same value, with the remaining two cards forming a pair. Ranked by the value of the three cards. Four of a Kind. Four cards with the same value. Ranked by the value of the four cards. Straight Flush. Five 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.

输入:

The input file contains several lines, each containing the designation of ten cards: the first five cards are the hand for the player named ``Black" and the next five cards are the hand for the player named ``White".

输出:

For each line of input, print a line containing one of the following: Black wins. White wins. Tie.

示例输入:

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

示例输出:

White wins.
Black wins.
Black wins.
Tie.

提示:

参考答案(内存最优[752]):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char poker[50];
int b[16],w[16],compb[6],compw[6];
int vb,vw;
int compler(const void*a,const void*b)
{
    return *(int *)b-*(int *)a;
}
void order()
{
    int i,j,k,k1,flag,two=0,three=0,four=0;
    vb=vw=1;
    if(poker[1]==poker[4]&&poker[4]==poker[7]&&poker[7]==poker[10]&&poker[10]==poker[13])
        vb=6;
    if(poker[16]==poker[19]&&poker[19]==poker[22]&&poker[22]==poker[25]&&poker[25]==poker[28])
        vw=6;
    memset(b,0,sizeof(b));
    memset(w,0,sizeof(w));
    memset(compb,0,sizeof(compb));
    memset(compw,0,sizeof(compw));
    for(i=0; i<13; i+=3)
    {
        if(poker[i]>='2'&&poker[i]<='9')
            b[poker[i]-'1'+1]++;
        else if(poker[i]=='T')
            b[10]++;
        else if(poker[i]=='J')
            b[11]++;
        else if(poker[i]=='Q')
            b[12]++;
        else if(poker[i]=='K')
            b[13]++;
        else if(poker[i]=='A')
            b[14]++;
    }
    for(i=15; i<28; i+=3)
    {
        if(poker[i]>='2'&&poker[i]<='9')
            w[poker[i]-'1'+1]++;
        else if(poker[i]=='T')
            w[10]++;
        else if(poker[i]=='J')
            w[11]++;
        else if(poker[i]=='Q')
            w[12]++;
        else if(poker[i]=='K')
            w[13]++;
        else if(poker[i]=='A')
            w[14]++;
    }
    k=k1=0;
    for(i=14;i>=2;i--)
    {
        for(j=0;j<b[i];j++)
            compb[k++]=i;
        for(j=0;j<w[i];j++)
            compw[k1++]=i;
    }
    for(i=0; i<5; i++)
    {
        k=1;
        for(j=i+1; j<5; j++)
        {
            if(compb[j]==compb[i])
                k++;
            else
            {
                i=j-1;
                break;
            }
        }
		
        if(k==2)
            two++;
        if(k==3)
            three++;
        if(k==4)
            four++;
		if(j==5)
			break;
    }
    if(four==1)
        vb=8;
    else if(two==1&&three==1)
        vb=7;
    if(vb==1)
    {
        if(three==1)
            vb=4;
        else if(two==2)
            vb=3;
        else if(two==1)
            vb=2;
    }
    flag=0;
    for(i=0; i<4; i++)
    {
        if(compb[i]-compb[i+1]!=1)
        {
            flag=1;
            break;
        }
    }
    if(flag==0&&vb<5)
        vb=5;
    else if(flag==0&&vb==6)
        vb=9;
    two=three=four=0;
    for(i=0; i<5; i++)
    {
        k=1;
        for(j=i+1; j<5; j++)
        {
            if(compw[j]==compw[i])
                k++;
            else
            {
                i=j-1;
                break;
            }
        }
		
        if(k==2)
            two++;
        if(k==3)
            three++;
        if(k==4)
            four++;
		if(j==5)
			break;
    }
    if(four==1)
        vw=8;
    else if(two==1&&three==1)
        vw=7;
    if(vw==1)
    {
        if(three==1)
            vw=4;
        else if(two==2)
            vw=3;
        else if(two==1)
            vw=2;
    }
    flag=0;
    for(i=0; i<4; i++)
    {
        if(compw[i]-compw[i+1]!=1)
        {
            flag=1;
            break;
        }
    }
    if(flag==0&&vw<5)
        vw=5;
    else if(flag==0&&vw==6)
        vw=9;
}
void maxorder()
{
	int i,j,k,t;
	if(vb==8)
	{
		if(compb[0]<compb[1]||compb[4]>compb[3])
		{	t=compb[0];compb[0]=compb[4];compb[4]=t;}
	}
	if(vb==7)
	{
		if(compb[1]!=compb[2])
		{
			t=compb[0];compb[0]=compb[4];compb[4]=t;
			t=compb[1];compb[1]=compb[3];compb[3]=t;
		}
	}
	if(vb==4)
	{
		if(compb[0]!=compb[1]&&compb[1]!=compb[2])
		{
			t=compb[0];compb[0]=compb[3];compb[3]=t;
			t=compb[1];compb[1]=compb[4];compb[4]=t;
		}
		else if(compb[0]!=compb[1]&&compb[1]==compb[2])
		{	t=compb[0];compb[0]=compb[3];compb[3]=t;}
	}
	if(vb==3)
	{
		if(compb[0]!=compb[1])
		{
			t=compb[0];
			for(i=1;i<5;i++)
				compb[i-1]=compb[i];
			compb[4]=t;
		}
		else if(compb[2]!=compb[3])
		{	t=compb[2];compb[2]=compb[4];compb[4]=t;}
	}
	if(vb==2)
	{
		if(compb[1]==compb[2])
		{	t=compb[2];compb[2]=compb[0];compb[0]=t;}
		else if(compb[3]==compb[2])
		{
			t=compb[2];compb[2]=compb[0];compb[0]=t;
			t=compb[3];compb[3]=compb[1];compb[1]=t;
		}
		else if(compb[3]==compb[4])	
		{
			t=compb[3];compb[3]=compb[1];compb[1]=t;
			t=compb[4];compb[4]=compb[0];compb[0]=t;
			t=compb[2];compb[2]=compb[4];compb[4]=t;
		}
	}


	if(vw==8)
	{
		if(compw[0]<compw[1]||compw[4]>compw[3])
		{	t=compw[0];compw[0]=compw[4];compw[4]=t;}
	}
	if(vw==7)
	{
		if(compw[1]!=compw[2])
		{
			t=compw[0];compw[0]=compw[4];compw[4]=t;
			t=compw[1];compw[1]=compw[3];compw[3]=t;
		}
	}
	if(vw==4)
	{
		if(compw[0]!=compw[1]&&compw[1]!=compw[2])
		{
			t=compw[0];compw[0]=compw[3];compw[3]=t;
			t=compw[1];compw[1]=compw[4];compw[4]=t;
		}
		else if(compw[0]!=compw[1]&&compw[1]==compw[2])
		{	t=compw[0];compw[0]=compw[3];compw[3]=t;}
	}
	if(vw==3)
	{
		if(compw[0]!=compw[1])
		{
			t=compw[0];
			for(i=1;i<5;i++)
				compw[i-1]=compw[i];
			compw[4]=t;
		}
		else if(compw[2]!=compw[3])
		{	t=compw[2];compw[2]=compw[4];compw[4]=t;}
	}
	if(vw==2)
	{
		if(compw[1]==compw[2])
		{	t=compw[2];compw[2]=compw[0];compw[0]=t;}
		else if(compw[3]==compw[2])
		{
			t=compw[2];compw[2]=compw[0];compw[0]=t;
			t=compw[3];compw[3]=compw[1];compw[1]=t;
		}
		else if(compw[3]==compw[4])	
		{
			t=compw[3];compw[3]=compw[1];compw[1]=t;
			t=compw[4];compw[4]=compw[0];compw[0]=t;
			t=compw[2];compw[2]=compw[4];compw[4]=t;
		}
	}

}

int main()
{
    int i;
    while(gets(poker))
    {
        order();
		maxorder();
        if(vb>vw)
            printf("Black wins.\n");
        else if(vb<vw)
            printf("White wins.\n");
        else
        {
            for(i=0; i<5; i++)
			{
				if(compb[i]!=compw[i])
				{
					if(compb[i]>compw[i])
						printf("Black wins.\n");
					else
						printf("White wins.\n");
					break;
				}
			}
			if(i==5)
				printf("Tie.\n");
        }
    }
    return 0;
}

参考答案(时间最优[0]):

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
    int num,mark;
} white[10],black[10];
int cmp(node a,node b)
{
    return a.num<b.num;
}
int  set(char c)
{
    if(c<='9' && c>='2')
        return c-'0';
    if(c == 'T')
        return 10;
    if(c == 'J')
        return 11;
    if(c == 'Q')
        return 12;
    if(c == 'K')
        return 13;
    if(c == 'A')
        return 14;
    if(c == 'C')
        return 1;
    if(c == 'D')
        return 2;
    if(c == 'H')
        return 3;
    if(c == 'S')
        return 4;
}

int up[10];

int same(node *a)
{
    int i,l = 0;
    for(i = 2; i<=5; i++)
        if(a[i].num!=a[i-1].num)
            up[l++] = i;
    return l;
}
int judge(node *a)
{
    int flag1 = 0;
    int flag2 = 0;
    if(a[1].mark == a[2].mark && a[2].mark == a[3].mark &&a[3].mark == a[4].mark &&a[4].mark == a[5].mark)
        flag1 = 1;
    if(a[1].num+1 == a[2].num && a[2].num+1 == a[3].num &&a[3].num+1 == a[4].num &&a[4].num+1 == a[5].num)
        flag2 = 1;
    if(flag1 && flag2)
        return 1;
   else  if(flag1 && !flag2)
        return 4;
    else if(flag2 && !flag1)
        return 5;
    int k = same(a);
    if(k == 0)
        return 2;
    else if(k == 1)
    {
        if(up[0] == 2 || up[0] == 5)
            return 2;
        if(up[0] == 3 || up[0] == 4)
            return 3;
    }
    else if(k == 2)
    {
        if(up[0] == 2 && up[1] == 3 || up[0] == 4 && up[1] == 5 || up[0] == 2 && up[1] == 5)
            return 6;
        else
            return 7;
    }
    else if(k == 3)
        return 8;
    else if(k == 4)
        return 9;
    return 0;
}

int find_one(node *a)
{
    int i;
    for(i = 1; i<=5; i++)
    {
        if(i == 1 && a[i].num!=a[i+1].num)
            return 1;
        if(i == 5 && a[i].num!=a[i-1].num)
            return 5;
        if(a[i].num!=a[i-1].num && a[i].num!=a[i+1].num)
            return i;
    }
    return 0;
}

int find_pair(node *a)
{
    int i;
    for(i = 1; i<=5; i++)
    {
        if(i == 1 && a[i].num == a[i+1].num)
            return 2;
        if(a[i].num == a[i-1].num)
            return i;
    }
    return 0;
}

int compare(int x)
{
    int i,j,a,b;
    if(x == 1 || x == 5)
    {
        if(white[5].num>black[5].num)
            return -1;
        else if(white[5].num<black[5].num)
            return 1;
        return 0;
    }
    else if(x == 2 || x == 3 || x == 6)
    {
        if(white[3].num>black[3].num)
            return -1;
        else if(white[3].num<black[3].num)
            return 1;
        return 0;
    }
    else if(x == 4 || x == 9)
    {
        for(i = 5; i>=1; i--)
        {
            if(black[i].num<white[i].num)return -1;
            else if(black[i].num>white[i].num)
                return 1;
        }
        return 0;
    }
    else if(x == 7)
    {
        if(white[4].num>black[4].num)
            return -1;
        else if(white[4].num<black[4].num)
            return 1;
        else
        {
            if(white[2].num>black[2].num)
                return -1;
            else if(white[2].num<black[2].num)
                return 1;
            int a = find_one(white);
            int b = find_one(black);
            if(black[b].num<white[a].num)
                return -1;
            else if(black[b].num>white[a].num)
                return 1;
            return 0;
        }
    }
    else if(x == 8)
    {
        int a = find_pair(white);
        int b = find_pair(black);
        int tem_w[10],tem_b[10],i,lb = 1,lw = 1;
        if(white[a].num>black[b].num)
            return -1;
        else if(white[a].num<black[b].num)
            return 1;
        for(i = 1; i<=5; i++)
        {
            if(i!=a && i!=a-1)
                tem_w[lw++] = white[i].num;
            if(i!=b && i!=b-1)
                tem_b[lb++] = black[i].num;
        }
        for(i = 3; i>=1; i--)
        {
            if(tem_w[i]>tem_b[i])
                return -1;
            else if(tem_b[i]>tem_w[i])
                return 1;
        }
        return 0;
    }
    return 0;
}

int main()
{
    char str[5];
    int sumb,sumw,j,k,i;
    while(~scanf("%2s",str))
    {
        j = 1;
        k = 1;
        black[j].num = set(str[0]);
        black[j++].mark = set(str[1]);
        for(i = 2; i<=10; i++)
        {
            scanf("%2s",str);
            if(i<6)
            {
                black[j].num = set(str[0]);
                black[j++].mark = set(str[1]);
            }
            else
            {
                white[k].num = set(str[0]);
                white[k++].mark = set(str[1]);
            }
        }
        sort(black+1,black+6,cmp);
        sort(white+1,white+6,cmp);
        sumb = judge(black);
        sumw = judge(white);
        if(sumb>sumw)
            printf("White wins.\n");
        else if(sumb<sumw)
            printf("Black wins.\n");
        else
        {
            int flag = compare(sumb);
            if(flag == -1)
                printf("White wins.\n");
            else if(flag == 1)
                printf("Black wins.\n");
            else
                printf("Tie.\n");
        }
    }

    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注