码迷,mamicode.com
首页 > 其他好文 > 详细

TOJ1290 Poker Hands 模拟题

时间:2017-01-24 21:52:31      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:bre   inf   scan   mem   char   include   car   names   break   

寒假期间抽空做的一道模拟题

难度不算大,把每种牌型分开处理,可以合并的步骤考虑合并。

代码比较丑陋,首次尝试Sport Programming的风格,结果搞了个不伦不类(手动笑哭)

  1 #include <algorithm>
  2 #include <bitset>
  3 #include <cctype>
  4 #include <complex>
  5 #include <cstdio>
  6 #include <cstring>
  7 #include <map>
  8 #include <queue>
  9 #include <set>
 10 #include <vector>
 11 
 12 using namespace std;
 13 
 14 const int inf = 0x3f3f3f3f;
 15 
 16 #define rep(id,from,to) for (int id = (from); id < (to); id++)
 17 #define irep(id,from,to) for (int id = (from); id > (to); id--)
 18 #define reset(arr,c) memset (arr, c, sizeof (arr) )
 19 #define grt(type) greater<type>
 20 #define Que(type) queue<type>
 21 #define Pque(type) priority_queue<type>
 22 #define gPque(type) priority_queue<type, vector<type>, greater<type> >
 23 
 24 
 25 struct Card
 26 {
 27     char suit;
 28     int val;
 29     bool read()
 30     {
 31         char ch[2];
 32         if (scanf ("%s", ch) == EOF) return false;
 33         suit = ch[1];
 34         if (ch[0] >= 2 && ch[0] <= 9) val = ch[0] - 0;
 35         else switch (ch[0])
 36             {
 37             case J:
 38                 val = 11;
 39                 break;
 40             case Q:
 41                 val = 12;
 42                 break;
 43             case K:
 44                 val = 13;
 45                 break;
 46             case A:
 47                 val = 14;
 48                 break;
 49             }
 50         return true;
 51     }
 52 };
 53 
 54 int isStraight (int* cnt, bool isFlush)
 55 {
 56     bool ok = true;
 57     int i;
 58     rep (j, 2, 15) if (cnt[j])
 59     {
 60         i = j;
 61         break;
 62     }
 63     rep (j, i + 1, i + 5) if (!cnt[j])
 64     {
 65         ok = false;
 66         break;
 67     }
 68     return ok ? (isFlush ? 8 : 4) : 0;
 69 }
 70 
 71 int isFourOfAKind (int* cnt)
 72 {
 73     bool ok = false;
 74     rep (i, 2, 15) if (cnt[i] == 4)
 75     {
 76         ok = true;
 77         break;
 78     }
 79     return ok ? 7 : 0;
 80 }
 81 
 82 int isFullHouse (int* cnt)
 83 {
 84     bool f3 = false, f2 = false;
 85     rep (i, 2, 15)
 86     {
 87         if (cnt[i] == 3) f3 = true;
 88         else if (cnt[i] == 2) f2 = true;
 89     }
 90     return f3 && f2 ? 6 : 0;
 91 }
 92 
 93 int isThreeOfAKind (int* cnt)
 94 {
 95     rep (i, 2, 15) if (cnt[i] == 3) return 3;
 96     return 0;
 97 }
 98 
 99 int isPair (int* cnt)
100 {
101     int res = 0;
102     rep (i, 2, 15) if (cnt[i] == 2) ++res;
103     return res;
104 }
105 
106 int cmpStraight (int* cntA, int* cntB)
107 {
108     int ma, mb;
109     irep (i, 14, 1) if (cntA[i])
110     {
111         ma = i;
112         break;
113     }
114     irep (i, 14, 1) if (cntB[i])
115     {
116         mb = i;
117         break;
118     }
119     if (ma > mb) return 1;
120     else if (ma < mb) return -1;
121     else return 0;
122 }
123 
124 int cmpFour (int* cntA, int* cntB)
125 {
126     int ma, mb;
127     rep (i, 2, 15) if (cntA[i] == 4)
128     {
129         ma = i;
130         break;
131     }
132     rep (i, 2, 15) if (cntB[i] == 4)
133     {
134         mb = i;
135         break;
136     }
137     if (ma > mb) return 1;
138     else if (mb > ma) return -1;
139     else return 0;
140 }
141 
142 int cmpFullHouse (int* cntA, int* cntB)
143 {
144     int ma, mb;
145     rep (i, 2, 15) if (cntA[i] == 3)
146     {
147         ma = i;
148         break;
149     }
150     rep (i, 2, 15) if (cntB[i] == 3)
151     {
152         mb = i;
153         break;
154     }
155     if (ma > mb) return 1;
156     else if (mb > ma) return -1;
157     else return 0;
158 }
159 
160 int cmpPair (int* cntA, int* cntB, int pr)
161 {
162     int pa = 0, ha = 0, pb = 0, hb = 0;
163     int ma[5] = {0}, mb[5] = {0};
164     irep (i, 14, 1)
165     {
166         if (cntA[i] == 2) ma[pa++] = i;
167         else if (cntA[i] == 1) ma[pr + (ha++)] = i;
168     }
169     irep (i, 14, 1)
170     {
171         if (cntB[i] == 2) mb[pb++] = i;
172         else if (cntB[i] == 1) mb[pr + (hb++)] = i;
173     }
174     rep (i, 0, 5)
175     {
176         if (ma[i] > mb[i]) return 1;
177         else if (mb[i] > ma[i]) return -1;
178     }
179     return 0;
180 }
181 
182 int cmp (Card* A, Card* B)
183 {
184     int cntA[15] = {0};
185     int cntB[15] = {0};
186     rep (i, 0, 5) ++cntA[A[i].val];
187     rep (i, 0, 5) ++cntB[B[i].val];
188     int typeA = 0, typeB = 0;
189     bool isFlushA = true, isFlushB = true;
190     rep (i, 1, 5) if (A[i].suit != A[0].suit)
191     {
192         isFlushA = false;
193         break;
194     }
195     rep (i, 1, 5) if (B[i].suit != B[0].suit)
196     {
197         isFlushB = false;
198         break;
199     }
200     typeA = isFlushA ? 5 : 0;
201     typeA = max (typeA, isStraight (cntA, isFlushA) );
202     typeA = max (typeA, isFourOfAKind (cntA) );
203     typeA = max (typeA, isFullHouse (cntA) );
204     typeA = max (typeA, isThreeOfAKind (cntA) );
205     typeA = max (typeA, isPair (cntA) );
206     typeB = isFlushA ? 5 : 0;
207     typeB = max (typeB, isStraight (cntB, isFlushB) );
208     typeB = max (typeB, isFourOfAKind (cntB) );
209     typeB = max (typeB, isFullHouse (cntB) );
210     typeB = max (typeB, isThreeOfAKind (cntB) );
211     typeB = max (typeB, isPair (cntB) );
212     if (typeA > typeB) return 1;
213     else if (typeA < typeB) return -1;
214     if (typeA == 4 || typeA == 8) return cmpStraight (cntA, cntB);
215     else if (typeA == 7) return cmpFour (cntA, cntB);
216     else if (typeA == 6 || typeA == 3) return cmpFullHouse (cntA, cntB);
217     else return cmpPair (cntA, cntB, (typeA == 5) ? 0 : typeA);
218     return 0;
219 }
220 
221 int main()
222 {
223     Card A[5], B[5];
224     while (A[0].read() )
225     {
226         rep (i, 1, 5) A[i].read();
227         rep (i, 0, 5) B[i].read();
228         int ans = cmp (A, B);
229         if (ans == 1) printf ("Black wins.\n");
230         else if (ans == -1) printf ("White wins.\n");
231         else printf ("Tie.\n");
232     }
233     return 0;
234 }

 

TOJ1290 Poker Hands 模拟题

标签:bre   inf   scan   mem   char   include   car   names   break   

原文地址:http://www.cnblogs.com/Onlynagesha/p/6347851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!