一道狗血的ACM題:Poker Hands

照着題目的輸入是正確的,但是我沒有測試過,但是提交上去是錯誤的,可能本人技術比較爛吧,提交的題目都是Wrong answer

Problem D

Poker Hands

Input: standard input

Output: standardoutput

Time Limit: 2 seconds

Memory Limit: 32 MB

 

A poker deck contains 52 cards - each card has a suit whichis one of clubs, diamonds, hearts, or spades (denoted C, D, H, and 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 asgiven 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 thefollowing 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 pokerhands and to indicate which, if either, has a higher rank.

Input

The input file contains severallines, each containing the designation of 10cards: the first 5 cards are thehand for the player named "Black"and the next 5 cards are the handfor the player named "White".

 

Output

For each line of input, print a line containing one of thefollowing three lines:

 
   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.

我寫的代碼:

#include <stdio.h>
#include <algorithm>
#include <cassert>
#include <cstring>
using namespace std;
#define  NCARDS 52
#define  NSUITS 4
char values[] = "23456789TJQKA";
char types[]  = "CDHS";
char white[] = "White wins.";
char black[] = "Black wins.";
char tie  [] = "Tie.";
int rank_card(char value , char type)
{
	int i,j;
	for (i = 0 ; i < NCARDS; ++i )
	{
		if (value == values[i])
		{
			for (j = 0 ; j < NSUITS ; ++j)
			{
				if (type == types[j])
				{
					return (i*NSUITS+j);
				}
			}
		}
	}
	fprintf(stderr,"bad card\n");
	return -1;
}
char card_type(int card)
{
	return types[card%4];
}
char card_value(int card)
{
	return values[card/4];
}
bool is_order(int arr[])
{
	char str[6] = "";
	int i = 0;
	for( ; i < 5 ; i++)
		str[i] = card_value(arr[i]);
	char* rc = search(values,values+13,str,str+5);
	return (rc!=values+13);
}
bool is_same_type(int arr[])
{
	int i = 1;
	for(; i < 5 ; i ++)
	{
		if(card_type(arr[i]) != card_type(arr[i-1]))
			return false;
	}
	return true;
}
int  get_level_type(int arr[])
{
	char ch = card_value(arr[0]);
	int level_type = 0;
	int i ;
	int nsame= 1;
	int ndouble = 0 , nthree = 0 , nfour = 0;
	for(i = 1; i < 5 ; ++i)
	{
		if(ch == card_value(arr[i]))
		{
			nsame++;
		}
		else
		{
			ch = card_value(arr[i]);
			if(nsame == 2)
			{
				ndouble++;
			}
			else if(nsame == 3)
			{
				nthree++;
			}
			else if (nsame == 4)
			{
				nfour++;
			}

			nsame = 1;

		}
	}
	if(nsame == 2)
	{
		ndouble++;
	}
	else if(nsame == 3)
	{
		nthree++;
	}
	else if (nsame == 4)
	{
		nfour++;
	}

	assert(!(nthree&&nfour));
	assert(ndouble < 3);
	if(nfour)
	{
		level_type = (1<<6);
		return level_type;
	}
	if(ndouble == 2)//雙對
	{
		level_type  = ndouble;
		return level_type;
	}
	else if(ndouble ==1)//一對
	{
		printf("yi dui \n");
		level_type = ndouble;
	}
	if(nthree)//三張
	{
		level_type |= (1<<2);
		if(ndouble ==1)
		{
			level_type |= (1<<5);//葫蘆
		}
		return level_type;
	}

	if(ndouble)		//如果 有一對 則不用檢查 順子和同花;
		return level_type;
	
	
	if(is_same_type(arr))//同花
	{

		level_type = (1<<4);
	}
	if (is_order(arr))//順子
	{

		level_type |= (1<<3);
	}
	if( (level_type & (1<<4))  && (level_type & (1<<3)) )
	{
		level_type |= (1<<7);
	}

	return level_type;
}
int same_level_cmp(int arr1[],int level_type1 , int arr2[],int leve_type2)
{
	int i = 0;
	int arr1_1,arr1_2,arr2_1,arr2_2;//檢測雙對
	if( !level_type1  || (level_type1&(1 << 4)) || (level_type1&(1<<3)))  //同花 順子 最大牌 歸爲一類 , 選擇兩幅牌的最大牌
	{
		for(i = 4 ; i >=0 ; i--)
		{
			if (card_value(arr1[i]) != card_value(arr2[i]))
			{
				return arr2[i]-arr1[i];
			}
		}
	}
	if( (level_type1&(1<<6)) || (level_type1&(1<<2)))  //四張 三張(包括葫蘆)  歸爲一類  只要檢測五張牌的中間那張
	{
		printf("duo zhang");
		return arr2[2] - arr1[2];
	}
	if( (level_type1&(1<<1)) ) //雙對
	{
		printf("shuang dui\n");
		arr1_1 = arr1[1];
		arr1_2 = arr1[3];
		if(arr1_1 > arr1_2) swap(arr1_1,arr1_2);

		arr2_1 = arr2[1];
		arr2_2 = arr2[3];
		if (arr2_1 > arr2_2) swap(arr2_1,arr2_2);
		
		if(arr1_2 != arr2_2) return arr2_2 - arr1_2;
		else				 return arr2_1 - arr1_1;
		
	}
	if( (level_type1&(1)) )//一對
	{
		printf("yi dui\n");
		for (i = 1 ; i < 5 ; i ++)
		{
			if (card_value(arr1[i]) == card_value(arr1[i-1]))
			{
				arr1_1 = arr1[i];
				break;
			}
		}

		for (i = 1; i < 5 ; i ++)
		{
			if(card_value(arr2[i]) == card_value(arr2[i-1]))
			{
				arr2_1 = arr2[i];
				break;
			}
		}
		if(arr1_1 != arr2_1) return arr1_1 - arr2_1;

		for (i = 4 ; i >= 0 ; i--)
		{
			if(card_value(arr1[i]) != card_value(arr2[i]))
				return arr2[i] - arr1[i];
		}
	}



		return 0;
}
int get_result(int arr1[] , int arr2[])
{
	int level_type1 = 0 , level_type2 = 0;
	sort(arr1,arr1+5);
	sort(arr2,arr2+5);
	level_type1 = get_level_type(arr1);
	level_type2 = get_level_type(arr2);
	if (level_type1 > level_type2)
	{
		return -1;
	}
	else if(level_type1 < level_type2)
	{
		return 1;
	}
	return  same_level_cmp( arr1,level_type1 ,  arr2, level_type2);
}
int main()
{
	char str[1000];
	char *p = NULL;
	int i = 0;
	int result = 0;
	memset(str,0,1000);
	int array[10] = {0};
	while(gets(str))
	{
		i = 0;
		p = strtok(str," ");
		while (p)
		{
			array[i++] = rank_card(p[0],p[1]);
			p = strtok(NULL," ");
		}

	//	result =get_result(array,array+5);
		if ( ( result=get_result(array,array+5)) < 0 )
		{
			printf("%s\n",black);
		}
		else if(result > 0)
		{
			printf("%s\n",white);
		}
		else
		{
			printf("%s\n",tie);
		}
		memset(str,0,1000);

	}

	return 0;
}

//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
// 2C 2D 2S 2H 3S 5S 6S 7D 8S AS



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章