-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2019-5-12-扑克牌排序.cpp
More file actions
75 lines (68 loc) · 1.4 KB
/
2019-5-12-扑克牌排序.cpp
File metadata and controls
75 lines (68 loc) · 1.4 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
#include<iostream>
#include<queue>
using namespace std;
typedef pair<int, int> pa;
struct cmp
{
bool operator() (pa a, pa b)
{
if (a.second > b.second)
return false;
else if (a.second == b.second)
{
if (a.first > b.first)
return false;
else if (a.first <= b.first)
return true;
}
else
return true;
}
};
int main()
{
//输入5张牌,只有两种情况,所有牌都不同,或有对子,有对子则对子放在前,均按大小排序。 2345678910JQKA
//输入为char类型,priority_queue中存pair,cmp比较函数,先比较个数,再比较大小。
vector<char> input = { '4', '4', '4', 'K', 'J' };
priority_queue<pa, vector<pa>, cmp> result;
int tmp[13] = { 0 };
for (int i = 0; i < input.size(); i++)
{
if (input[i] == 'J')
tmp[9]++;
else if (input[i] == 'Q')
tmp[10]++;
else if (input[i] == 'K')
tmp[11]++;
else if (input[i] == 'A')
tmp[12]++;
else
tmp[input[i] - '2']++;
}
for (int i = 0; i < 13; i++)
if (tmp[i] != 0)
{
#if _DEBUG
cout << i << " : " << tmp[i] << endl;
#endif
result.push(pa(i, tmp[i]));
}
while (!result.empty())
{
for (int i = 0; i < result.top().second; i++)
{
if (result.top().first == 9)
cout << "J ";
else if (result.top().first == 10)
cout << "Q ";
else if (result.top().first == 11)
cout << "K ";
else if (result.top().first == 12)
cout << "A ";
else
cout << result.top().first + 2 << " ";
}
result.pop();
}
return 0;
}