# 扑克牌输赢判断系统（景驰18年秋招第一题）

### 题目描述

5张牌所能构成的牌组有以下几种情况：

3
2 3 4 5 6
4 4 4 4 11
2 3 8 9 10
10 9 8 3 2
4 4 7 8 7
4 4 8 7 8

2
0
2

### 代码

#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

enum Result{EQUAL, WIN, LOSE};
enum CardStyle{SINGLE, ONE_PAIR, TWO_PAIR, THREE, CONTINUES, CUCURBIT, FOUR};

CardStyle get_CardStyle(map<int, int>& _map)
{
int map_size = _map.size();
if (map_size == 5){
//sort(_map.begin(), _map.end());
auto ite1 = _map.begin();
auto ite2 = _map.begin();
while (++ite2 != _map.end()){
if (ite2->first != (ite1->first + 1))
break;
++ite1;
}
if (ite2 == _map.end())
return CONTINUES;
else{
ite1 = _map.end();
--ite1;
if (ite1->first == 14){
ite2 = _map.begin();
int i = 0;
for (; i < 4; ++i){
if (ite2->first != (i + 2))
break;
++ite2;
}
if (i == 4){
_map.erase(ite1);
return CONTINUES;
}
}
return SINGLE;
}
}
else if (map_size == 4)
return ONE_PAIR;
else if (map_size == 3){
for (auto val : _map){
if (val.second == 2)
return TWO_PAIR;
else if (val.second == 3)
return THREE;

}
}
else if (map_size == 2){
for (auto val : _map){
if (val.second == 2)
return CUCURBIT;
else if (val.second == 3)
return CUCURBIT;
else if (val.second == 4)
return FOUR;
}
}
}
Result compare(int* a, int* b, int num)
{
if (num == 0)
return EQUAL;
for (int i = 0; i < num; ++i){
if (a[i] > b[i])
return WIN;
else if (a[i] < b[i])
return LOSE;
else{
return compare(++a, ++b, --num);
}
}
}

Result cards_judge(int* a, int* b)
{
map<int, int> a_map;
map<int, int> b_map;
for (int i = 0; i < 5; ++i){
++a_map[a[i]];
++b_map[b[i]];
}
CardStyle a_style = get_CardStyle(a_map);
CardStyle b_style = get_CardStyle(b_map);
if (a_style > b_style)
return WIN;
else if (a_style < b_style)
return LOSE;
else{
auto ite_a = a_map.begin();
auto ite_b = b_map.begin();
int a_pair[5];
int b_pair[5];
int ai, bi = 3;
switch (a_style){
case SINGLE:
ai = 4;
bi = 4;
while (ite_a != a_map.end()){
a_pair[ai--] = ite_a->first;
b_pair[bi--] = ite_b->first;
++ite_a;
++ite_b;
}
return compare(a_pair, b_pair, 5);
break;
case ONE_PAIR:
ai = 3;
bi = 3;
while (ite_a != a_map.end()){
if (ite_a->second == 1)
a_pair[ai--] = ite_a->first;
else
a_pair[0] = ite_a->first;
if (ite_b->second == 1)
b_pair[bi--] = ite_b->first;
else
b_pair[0] = ite_b->first;
++ite_a;
++ite_b;
}
return compare(a_pair, b_pair, 4);
break;
case TWO_PAIR:
ai = 1;
bi = 1;
while (ite_a != a_map.end()){
if (ite_a->second == 2)
a_pair[ai--] = ite_a->first;
else
a_pair[2] = ite_a->first;
if (ite_b->second == 2)
b_pair[bi--] = ite_b->first;
else
b_pair[2] = ite_b->first;
++ite_a;
++ite_b;
}
return compare(a_pair, b_pair, 3);
break;
case THREE:
ai = 2;
bi = 2;
while (ite_a != a_map.end()){
if (ite_a->second == 1)
a_pair[ai--] = ite_a->first;
else
a_pair[0] = ite_a->first;
if (ite_b->second == 1)
b_pair[bi--] = ite_b->first;
else
b_pair[0] = ite_b->first;
++ite_a;
++ite_b;
}
return compare(a_pair, b_pair, 3);
break;
case CONTINUES:
ite_a = a_map.end();
ite_b = b_map.end();
--ite_a;
--ite_b;
if (ite_a->first > ite_b->first)
return WIN;
else if (ite_a->first < ite_b->first)
return LOSE;
else
return EQUAL;
break;
break;
case CUCURBIT:
while (ite_a != a_map.end()){
if (ite_a->second == 2)
a_pair[1] = ite_a->first;
else
a_pair[0] = ite_a->first;
if (ite_b->second == 2)
b_pair[1] = ite_b->first;
else
b_pair[0] = ite_b->first;
++ite_a;
++ite_b;
}
return compare(a_pair, b_pair, 2);
return WIN;
case FOUR:
while (ite_a != a_map.end()){
if (ite_a->second == 1)
a_pair[1] = ite_a->first;
else
a_pair[0] = ite_a->first;
if (ite_b->second == 1)
b_pair[1] = ite_b->first;
else
b_pair[0] = ite_b->first;
++ite_a;
++ite_b;
}
return compare(a_pair, b_pair, 2);
break;
}
}
}

int main(int argc, char** argv)
{
int n = 0;
int number[2][5] = {0};
cin >> n;
if (n > 100 || n <= 0)
return 1;
int* result = new int[n];
for (int i = 0; i < n; ++i){
for (int i = 0; i < 2; ++i){
for (int j = 0; j < 5; ++j){
cin >> number[i][j];
}
}
result[i] = cards_judge(number[0], number[1]);
}
for (int i = 0; i < n; ++i){
cout << result[i] << endl;
}
delete[] result;
return 0;
}

### 思路

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客