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 }
时间: 2024-12-10 01:39:20

ZOJ 1111 Poker Hands的相关文章

ZOJ 1111 Poker Hands --复杂模拟

昨天晚上写的,写了一个多小时,9000+B,居然1A了,爽. 题意:玩扑克,比大小.规则如下: 题意很简单,看过赌神的人都知道,每人手中5张排,比牌面大小,牌面由大到小分别是(这里花色无大小),级别从高到低依次为:1.同花顺:牌面一样,只比较最大的看谁大,一样大则平手2.四条:四个一样的,看这四个一样的中谁大谁赢3.葫芦:三条+一对,看三条中谁的牌面大4.同花:都是同花就按从大到小比较,看谁的更大5.顺子:都是顺子看最大的谁大,一样则平手6.三条:三条看三条中谁的牌面大7.两对: 两对就看谁的对

[ZOJ 3839] Poker Face (递归)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3839 题目大意:画脸..每张脸是上一个脸倒过来加上眼睛.. 注意n<8时停止,被这个坑惨了- -以为是0停止.. 递归,然后去推坐标公式.. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <vector> 5 usi

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

ZOJ 3864 Quiz for EXO-L

题目链接:Quiz for EXO-L 题面: B - Quiz for EXO-L Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3864 Description Exo (Korean: ??; Chinese:爱咳嗽; often stylized as EXO) is a Chinese-South Korean boy band

ZOJ 1093 &amp;&amp; NYoj16(DP)

~~~~ 两个题目大致类似,NYOJ上面那道题就是小白上的矩形嵌套啦. 都是先对长宽进行排序,然后逐层更新最大值(边更新边记录). 好了,不说了. 题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1093 http://acm.nyist.net/JudgeOnline/problem.php?pid=16 ~~~~ ZOJ1093: #include<cstdio> #include<cstring&

CSU 1111: 三家人【有趣的思维题】

1111: 三家人 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 2241  Solved: 874 [Submit][Status][Web Board] Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列,便出了90元.请问这笔钱如何分给A.B 二位太太较为恰当?A 应得多少元?90/(5+4)*5=$50

作业:JavaScript(数组篇-poker)给我的徒弟出个题。。。记得早点写完,然后大家3人可以早点打牌了

吐槽一下:“今天实际上我左思右想,写个什么东西好呢!手上的笔转了半天....最后还是给自己留点余地!看着他们什么酒店管理系统,呼叫中心系统之类的....简直是把自己固定死了!感觉一撸到底的感觉!!!我们是程序员所以我觉得要思想灵活点HOHO...” 今天只是想写一篇关于JavaScript数组的一篇文章 以前我认为我已经完全把数组掌握了!但是去年面试的时候被问呆了!!瞬间感觉自己萌萌哒!!所以把书看完了不算会!所以这次为了让我的徒弟能够不再犯我当年的错误...哼哼!我决定让她来一次实战!!!不能