forked from wandering007/ProjectEuler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP54.cpp
More file actions
204 lines (196 loc) · 4.48 KB
/
P54.cpp
File metadata and controls
204 lines (196 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include<iostream>
#include<fstream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<algorithm>
#include<math.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iomanip>
#define MAXN 10000000
#define MOD 10000000000
#define LL long long
#define eps 1e-8
#define inf 0x3f3f3f3f
using namespace std;
unsigned int rec[14];
char P1[6][3], P2[6][3];
enum Rank{HighCard, OnePair, TwoPairs, ThreeOfaKind, Straight, Flush,
FullHouse, FourOfaKind, StraightFlush, RoyalFlush};
void adjust(char* V)
{
switch(V[0])
{
case 'T': V[0] = '9' + 1; break;
case 'J': V[0] = '9' + 2; break;
case 'Q': V[0] = '9' + 3; break;
case 'K': V[0] = '9' + 4; break;
case 'A': V[0] = '9' + 5; break;
default: {;}
}
return;
}
int JudgeRank(char P[][3])
{
memset(rec, 0, sizeof(rec));
for(int i = 1; i <= 5; i++)
rec[P[i][0] - '2']++;
int NumOfPairs = 0;
bool NumOfThr = false;
for(int i = 0; i < 14; i++)
{
switch(rec[i])
{
case 2: NumOfPairs++; break;
case 3: NumOfThr = true; break;
case 4: return FourOfaKind;
}
}
if(1 == NumOfPairs)
{
if(NumOfThr)
return FullHouse;
return OnePair;
}
if(2 == NumOfPairs)
return TwoPairs;
if(NumOfThr)
return ThreeOfaKind;
bool isSameSuit = true, isConseVal = true;
for(int i = 1; i < 5; i++)
{
if(P[i][0] + 1 != P[i + 1][0])
isConseVal = false;
if(P[i][1] != P[i + 1][1])
isSameSuit = false;
}
if(isSameSuit)
{
if(isConseVal)
{
if('9' + 1 == P[1][0])
return RoyalFlush;
return StraightFlush;
}
return Flush;
}
else if(isConseVal)
return Straight;
return HighCard;
}
int ValOfOnePair(char P[][3])
{
for(int i = 1; i < 5; i++)
if(P[i][0] == P[i + 1][0])
return P[i][0];
return 0;//impossible
}
int ValOfTwoPairs(char P[][3])
{//±àÂë³É14½øÖÆÊý
int val = 0;
bool isMax = true;
for(int i = 5; i >= 1; i--)
{
if(i > 1 && P[i][0] == P[i - 1][0])
{
i--;
if(isMax)
{
val += (P[i][0] - '1') * 14 * 14;
isMax = false;
}
else
{
val += (P[i][0] - '1') * 14;
}
}
else val += P[i][0] - '1';
}
return val;
}
bool isPlayerOneWin()
{
int Level1 = JudgeRank(P1);
int Level2 = JudgeRank(P2);
if(Level1 < Level2)
return false;
if(Level1 > Level2)
return true;
int v1, v2;
switch(Level1)
{
case OnePair:
case ThreeOfaKind:
case FourOfaKind:
v1 = ValOfOnePair(P1);
v2 = ValOfOnePair(P2);
if(v1 != v2)
return v1 > v2;
break;
case TwoPairs:
v1 = ValOfTwoPairs(P1);
v2 = ValOfTwoPairs(P2);
return v1 > v2;
}
for(int i = 5; i > 0; i--)
{
if(P1[i][0] > P2[i][0])
return true;
if(P1[i][0] < P2[i][0])
return false;
}
return false;//impossible
}
void BubbleSort(char P[][3], int n)
{
int i = n, LastExcchangeIndex;
while(i > 1)
{
LastExcchangeIndex = 1;
for(int j = 1; j < i; j++)
{
if(P[j][0] > P[j + 1][0])
{
strcpy(P[0], P[j + 1]);
strcpy(P[j + 1], P[j]);
strcpy(P[j], P[0]);
LastExcchangeIndex = j;
}
}
i = LastExcchangeIndex;
}
}
int main()
{
clock_t start = clock();
freopen("p054_poker.txt", "r", stdin);
int cnt = 0;
for(int i = 1; i <= 1000; i++)
{
for(int j = 1; j <= 5; j++)
{
scanf("%s", P1[j]);
adjust(P1[j]);
}
for(int j = 1; j <= 5; j++)
{
scanf("%s", P2[j]);
adjust(P2[j]);
}
BubbleSort(P1, 5);
BubbleSort(P2, 5);
if(isPlayerOneWin())
cnt++;
}
printf("%d\n", cnt);
printf("time cost: %lf", (double)( clock() - start) / CLOCKS_PER_SEC);
return 0;
}