ZOJ 1111 Poker Hands

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1111

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. 

题意:两手扑克牌,一手各5张牌,按照接下来的规则比大小。下面的规则是按照一手牌从大到小介绍的。

  规则一:5张牌花色一样,大小连着的,俗称“同花顺”。同花顺的大小根据最大值的判断。

  规则二:有4张牌一样大的。就是说5张牌中能组成炸弹。这样的牌通过那4张牌的大小再来进行比较大小。比如(A,3,3,3,3)和(9,4,4,4,4)就是后者大。

  规则三:3张牌一样大,另外两张牌组成一对。满足规则三的两手牌根据那3张牌再进行大小比较。

  规则四:5张牌同色,大小比较就看牌的大小了。

  规则五:5张牌大小连着组成了顺子,大小看最大值。

  规则六:3张牌相同,大小看3张牌的值的大小。

  规则七:组成两对和一张单牌,大小看那两对的大小,如果还相同的就看那单牌。

  规则八:组成一队,大小先看这一对,然后再比较剩下的三张散牌。

  规则九:5张牌没什么组合的,散牌,就单纯的比较大小。

解法:按照这几个规则模拟即可。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 using namespace std;
  8 struct node
  9 {
 10     int value;
 11     char color;
 12     friend bool operator <(node a,node b)
 13     {
 14         return a.value > b.value;
 15     }
 16 }an[6],bn[6];
 17 int cmp(int i,int j) {return i<j; }
 18 int getvalue(char *str)
 19 {
 20     if (str[0]>='2'&&str[0]<='9') return str[0]-'0';
 21     if (str[0]=='T') return 10;
 22     if (str[0]=='J') return 11;
 23     if (str[0]=='Q') return 12;
 24     if (str[0]=='K') return 13;
 25     if (str[0]=='A') return 14;
 26 }
 27 int color_ok(node *cn,int k)
 28 {
 29     int flag=1;
 30     for (int i=2 ;i<=5 ;i++) if (cn[i].color!=cn[1].color) flag=0;
 31     for (int i=1 ;i<=4 ;i++) if (cn[i].value!=cn[i+1].value+1) flag=0;
 32     if (cn[1].value!=k) flag=0;
 33     if (flag) return 1;
 34     return 0;
 35 }
 36 int four_ok(node *cn,int k)
 37 {
 38     int cnt=0;
 39     for (int i=1 ;i<=5 ;i++) if (cn[i].value==k) cnt++;
 40     if (cnt==4) return 1;
 41     return 0;
 42 }
 43 int full_ok(node *cn,int k)
 44 {
 45     int cnt=0;
 46     int u=0,v=0;
 47     for (int i=1 ;i<=5 ;i++)
 48     {
 49         if (cn[i].value==k) cnt++;
 50         else if (!u) u=i;
 51         else v=i;
 52     }
 53     if (cnt==3)
 54     {
 55         if (cn[u].value==cn[v].value) return 1;
 56         else return 0;
 57     }
 58     else return 0;
 59 }
 60 int same_color(node *cn)
 61 {
 62     for (int i=2 ;i<=5 ;i++) if (cn[i].color!=cn[1].color) return 0;
 63     return 1;
 64 }
 65 int straight_ok(node *cn,int k)
 66 {
 67     int flag=0;
 68     for (int i=1 ;i<=4 ;i++) if (cn[i].value!=cn[i+1].value+1) flag=1;
 69     if (cn[1].value!=k) flag=1;
 70     if (flag) return 0;
 71     return 1;
 72 }
 73 int three_ok(node *cn,int k)
 74 {
 75     int cnt=0;
 76     for (int i=1 ;i<=5 ;i++) if (cn[i].value==k) cnt++;
 77     if (cnt==3) return 1;
 78     return 0;
 79 }
 80 int first_ok(node *cn,int k)
 81 {
 82     int cnt=0;
 83     for (int i=1 ;i<=5 ;i++) if (cn[i].value==k) cnt++;
 84     if (cnt==2) return 1;
 85     return 0;
 86 }
 87 int main()
 88 {
 89     char str[4];
 90     while (scanf("%s",str)!=EOF)
 91     {
 92         an[1].value=getvalue(str);an[1].color=str[1];
 93         for (int i=2 ;i<=5 ;i++)
 94         {
 95             scanf("%s",str);
 96             an[i].value=getvalue(str);
 97             an[i].color=str[1];
 98         }
 99         for (int i=1 ;i<=5 ;i++)
100         {
101             scanf("%s",str);
102             bn[i].value=getvalue(str);
103             bn[i].color=str[1];
104         }
105         sort(an+1,an+5+1);sort(bn+1,bn+5+1);
106         int OK=0;
107         int flag1=0,flag2=0;
108         ///Straight flush
109         for (int i=14 ;i>=6 ;i--)
110         {
111             flag1=flag2=0;
112             if (color_ok(an,i)) flag1=1;
113             if (color_ok(bn,i)) flag2=1;
114             if (flag1 && flag2) {printf("Tie.\n");OK=1;break; }
115             if (flag1) {printf("Black wins.\n");OK=1;break; }
116             if (flag2) {printf("White wins.\n");OK=1;break; }
117         }
118         if (OK) continue;
119         ///Four of a kind
120         for (int i=14 ;i>=2 ;i--)
121         {
122             flag1=flag2=0;
123             if (four_ok(an,i)) flag1=1;
124             if (four_ok(bn,i)) flag2=1;
125             if (flag1 && flag2) {printf("Tie.\n");OK=1;break; }
126             if (flag1) {printf("Black wins.\n");OK=1;break; }
127             if (flag2) {printf("White wins.\n");OK=1;break; }
128         }
129         if (OK) continue;
130         ///Full House
131         for (int i=14 ;i>=2 ;i--)
132         {
133             flag1=flag2=0;
134             if (full_ok(an,i)) flag1=1;
135             if (full_ok(bn,i)) flag2=1;
136             if (flag1 && flag2) {printf("Tie.\n");OK=1;break; }
137             if (flag1) {printf("Black wins.\n");OK=1;break; }
138             if (flag2) {printf("White wins.\n");OK=1;break; }
139         }
140         if (OK) continue;
141         ///Flush
142         flag1=flag2=0;
143         if (same_color(an)) flag1=1;
144         if (same_color(bn)) flag2=1;
145         if (flag1 && flag2)
146         {
147             int ok=0;
148             for (int i=1 ;i<=5 ;i++)
149             {
150                 if (an[i].value>bn[i].value) {printf("Black wins.\n");ok=1;OK=1;break; }
151                 else if (an[i].value<bn[i].value) {printf("White wins.\n");ok=1;OK=1;break; }
152             }
153             if (!ok) {printf("Tie.\n");OK=1;continue;}
154         }
155         if (flag1&&!OK) {printf("Black wins.\n");OK=1;continue; }
156         if (flag2&&!OK) {printf("White wins.\n");OK=1;continue; }
157         if (OK) continue;
158         ///Straight
159         for (int i=14 ;i>=6 ;i--)
160         {
161             flag1=flag2=0;
162             if (straight_ok(an,i)) flag1=1;
163             if (straight_ok(bn,i)) flag2=1;
164             if (flag1 && flag2) {printf("Tie.\n");OK=1;break; }
165             if (flag1) {printf("Black wins.\n");OK=1;break; }
166             if (flag2) {printf("White wins.\n");OK=1;break; }
167         }
168         if (OK) continue;
169         ///Three of a Kind
170         for (int i=14 ;i>=2 ;i--)
171         {
172             flag1=flag2=0;
173             if (three_ok(an,i)) flag1=1;
174             if (three_ok(bn,i)) flag2=1;
175             if (flag1 && flag2) {printf("Tie.\n");OK=1;break; }
176             if (flag1) {printf("Black wins.\n");OK=1;break; }
177             if (flag2) {printf("White wins.\n");OK=1;break; }
178         }
179         if (OK) continue;
180         ///Two Pairs
181         for (int i=14 ;i>=2 ;i--)
182         {
183             flag1=flag2=0;
184             int k1=0,k2=0;
185             if (first_ok(an,i))
186             {
187                 for (int j=i-1 ;j>=2 ;j--)
188                 {
189                     if (first_ok(an,j)) {flag1=1;k1=j;break;}
190                 }
191             }
192             if (first_ok(bn,i))
193             {
194                 for (int j=i-1 ;j>=2 ;j--)
195                 {
196                     if (first_ok(bn,j)) {flag2=1;k2=j;break;}
197                 }
198             }
199             if (flag1 && flag2 && k1==k2)
200             {
201                 int u=0,v=0;
202                 for (int j=1 ;j<=5 ;j++) if (an[j].value!=i && an[j].value!=k1) {u=j;break; }
203                 for (int j=1 ;j<=5 ;j++) if (bn[j].value!=i && bn[j].value!=k2) {v=j;break; }
204                 if (an[u].value==bn[v].value) {printf("Tie.\n");OK=1;break; }
205                 else if (an[u].value>bn[v].value) {printf("Black wins.\n");OK=1;break; }
206                 else {printf("White wins.\n");OK=1;break; }
207             }
208             if (flag1 && flag2 && !OK && k1!=k2)
209             {
210                 if (k1>k2) {printf("Black wins.\n");OK=1;break; }
211                 else {printf("White wins.\n");OK=1;break; }
212             }
213             if (flag1) {printf("Black wins.\n");OK=1;break; }
214             if (flag2) {printf("White wins.\n");OK=1;break; }
215         }
216         if (OK) continue;
217         ///Pair
218         for (int i=14 ;i>=2 ;i--)
219         {
220             flag1=flag2=0;
221             if (first_ok(an,i)) flag1=1;
222             if (first_ok(bn,i)) flag2=1;
223             if (flag1 && flag2)
224             {
225                 int a[4],b[4];
226                 int c=0,d=0;
227                 for (int j=1 ;j<=5 ;j++) if (an[j].value!=i) a[c++]=an[j].value;
228                 for (int j=1 ;j<=5 ;j++) if (bn[j].value!=i) b[d++]=bn[j].value;
229                 sort(a,a+c,cmp);sort(b,b+d,cmp);
230                 int flag=0;
231                 for (int j=0 ;j<3 ;j++)
232                 {
233                     if (a[j]>b[j]) {printf("Black wins.\n");flag=1;OK=1;break; }
234                     else if (a[j]<b[j]) {printf("White wins.\n");flag=1;OK=1;break; }
235                 }
236                 if (flag==0) {printf("Tie.\n");OK=1;break; }
237             }
238             if (flag1&&!OK) {printf("Black wins.\n");OK=1;break; }
239             if (flag2&&!OK) {printf("White wins.\n");OK=1;break; }
240         }
241         if (OK) continue;
242         ///High Card
243         int flag=0;
244         for (int i=1 ;i<=5 ;i++)
245         {
246             if (an[i].value>bn[i].value) {printf("Black wins.\n");flag=1;OK=1;break; }
247             else if (an[i].value<bn[i].value) {printf("White wins.\n");flag=1;OK=1;break; }
248         }
249         if (!flag) {printf("Tie.\n");continue; }
250     }
251     return 0;
252 }

 

posted @ 2014-09-28 21:28  huangxf  阅读(390)  评论(0编辑  收藏  举报