思路&题解

PC/UVA 110202/10315
本题。按顺序比较牌型。

//author: CHC
//First Edit Time: 2014-01-16 14:29
//Last Edit Time: 2014-01-16 21:09
//Filename:1.cpp
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
char tran[]={
'0','2','3','4','5','6','7','8','9','T','J','Q','K','A' };
char s[1000];
int getnum(char ch){
for(int i=0;i<14;i++)
if(ch==tran[i])return i;
return 0;
}
struct Plate{
int num;
char se;
};
struct Splate{
Plate pai[5];
char tag[10];//九种牌型
}cs[2];
int cmp1(Plate x,Plate y){
return x.num<y.num;
}
int cmp2(Plate x,Plate y){
return x.se<y.se;
}
int checkths(Splate x)//同花顺
{
char ch=x.pai[0].se;
for(int i=1;i<5;i++)
if(ch!=x.pai[i].se)return -1;
sort(x.pai,x.pai+5,cmp1);
for(int i=1;i<5;i++)
if(x.pai[i].num!=1+x.pai[i-1].num)return -1;
return x.pai[4].num;
}
int checksiz(Splate x)//四张
{
sort(x.pai,x.pai+5,cmp1);
int ha[14]={ 0 };
for(int i=0;i<5;i++)
++ha[x.pai[i].num];
for(int i=0;i<14;i++)
if(ha[i]==4)return i;
return -1;
}
int checkhl(Splate x)//葫芦
{
sort(x.pai,x.pai+5,cmp1);
int ha[14]={ 0 };
for(int i=0;i<5;i++)++ha[x.pai[i].num];
int san=-1,er=-1;
for(int i=0;i<14;i++)
if(ha[i]==2)er=i;
else if(ha[i]==3)san=i;
if(er>=0&&san>=0)return san;
return -1;
}
int checkth(Splate x,int *p)//同花
{
char ch=x.pai[0].se;
for(int i=1;i<5;i++)
if(ch!=x.pai[i].se)return -1;
sort(x.pai,x.pai+5,cmp1);
for(int i=4;i>=0;i--)p[4-i]=x.pai[i].num;
return 1;
}
int checksunzi(Splate x)//顺子
{
sort(x.pai,x.pai+5,cmp1);
for(int i=1;i<5;i++)
if(x.pai[i].num!=x.pai[i-1].num+1)return -1;
return x.pai[4].num;
}
int checksanz(Splate x)//三张
{
int ha[14]={ 0 };
for(int i=0;i<5;i++)++ha[x.pai[i].num];
for(int i=0;i<14;i++)
if(ha[i]==3)return i;
return -1;
}
int checkld(Splate x,int *p)//两对
{
int ha[14]={ 0 };
for(int i=0;i<5;i++)++ha[x.pai[i].num];
int y=0,er=0;
for(int i=0;i<14;i++)
{
if(ha[i]==1){
++y;p[2]=i;
}
else if(ha[i]==2){
++er;
if(er==2){
if(i>p[0]){
p[1]=p[0];
p[0]=i;
}
else p[1]=i;
continue;
}
p[0]=i;
}
}
if(y==1&&er==2)return 1;
return -1;
}
int checkyd(Splate x,int *p)//一对
{
sort(x.pai,x.pai+5,cmp1);
int ha[14]={ 0 };
for(int i=0;i<5;i++)++ha[x.pai[i].num];
p[0]=-1;
for(int i=0;i<14;i++)
if(ha[i]==2)
{
p[0]=i;
//break;
}
if(p[0]==-1)return -1;
int k=0;
for(int i=4;i>=0;i--)
if(x.pai[i].num!=p[0])p[++k]=x.pai[i].num;
return 1;
}
int checkzdp(Splate x,int *p)//最大牌
{
sort(x.pai,x.pai+5,cmp1);
for(int i=4;i>=0;i--)
p[4-i]=x.pai[i].num;
return 1;
}
void whowin(int b,int w)
{
if(b>w) puts("Black wins.");
else if(b<w) puts("White wins.");
else puts("Tie.");
}
void dosome()//处理
{
int w,b,aa[6],bb[6];
b=w=-1;
b=checkths(cs[0]);
w=checkths(cs[1]);
//puts("in ths");
if(b>=0||w>=0){
whowin(b,w);
return ;
}
b=w=-1;
b=checksiz(cs[0]);
w=checksiz(cs[1]);
//puts("in siz");
if(b>=0||w>=0){
whowin(b,w);
return ;
}
b=w=-1;
b=checkhl(cs[0]);
w=checkhl(cs[1]);
//puts("in hl");
if(b>=0||w>=0){
whowin(b,w);
return ;
}
b=w=-1;
b=checkth(cs[0],aa);
w=checkth(cs[1],bb);
//puts("in th");
if(b>=0||w>=0){
if(b>w) {
puts("Black wins.");
return ;
}
else if(b<w) {
puts("White wins.");
return ;
}
else
for(int i=0;i<5;i++)
if(aa[i]>bb[i]){
puts("Black wins.");
return ;
}
else if(aa[i]<bb[i]){
puts("White wins.");
return ;
}
puts("Tie.");
return ;
}
b=w=-1;
b=checksunzi(cs[0]);
w=checksunzi(cs[1]);
//puts("in sunzi");
if(b>=0||w>=0){
whowin(b,w);
return;
}
b=w=-1;
b=checksanz(cs[0]);
w=checksanz(cs[1]);
//puts("in sanz");
if(b>=0||w>=0){
whowin(b,w);
return ;
}
b=w=-1;
b=checkld(cs[0],aa);
w=checkld(cs[1],bb);
//puts("in ld");
if(b>=0||w>=0){
if(b>w) {
puts("Black wins.");
return ;
}
else if(b<w) {
puts("White wins.");
return ;
}
else
for(int i=0;i<3;i++)
if(aa[i]>bb[i]){
puts("Black wins.");
return ;
}
else if(aa[i]<bb[i]){
puts("White wins.");
return ;
}
puts("Tie.");
return ;
}
b=w=-1;
b=checkyd(cs[0],aa);
w=checkyd(cs[1],bb);
//puts("in yd");
if(b>=0||w>=0){
if(b>w) {
puts("Black wins.");
return ;
}
else if(b<w) {
puts("White wins.");
return ;
}
else
for(int i=0;i<4;i++)
if(aa[i]>bb[i]){
puts("Black wins.");
return ;
}
else if(aa[i]<bb[i]){
puts("White wins.");
return ;
}
puts("Tie.");
return ;
}
b=w=-1;
b=checkzdp(cs[0],aa);
w=checkzdp(cs[1],bb);
//puts("in zdp");
for(int i=0;i<5;i++)
if(aa[i]>bb[i]){
puts("Black wins.");
return ;
}
else if(aa[i]<bb[i]){
puts("White wins.");
return ;
}
puts("Tie.");
return ;
}
int main()
{
while(1){
int flag=0;
memset(cs,0,sizeof(cs));
for(int i=0;i<2&&flag!=EOF;i++){
for(int j=0;j<5&&flag!=EOF;j++){
flag=scanf(" %s",s);
cs[i].pai[j].num=getnum(s[0]);
cs[i].pai[j].se=s[1];
}
}
if(flag==EOF)break;
dosome();
}
return 0;
}

Poker Hands

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.

Input

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”.

Output

For each line of input, print a line containing one of the following:
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.