回答編集履歴
1
追記
answer
CHANGED
@@ -55,4 +55,76 @@
|
|
55
55
|
10 cards: HJ SA D7 DA C2 SJ HQ DJ CK H3
|
56
56
|
10 cards: C7 S2 DT ST SQ C9 H5 DQ C3 SK
|
57
57
|
*/
|
58
|
+
```
|
59
|
+
|
60
|
+
[追記] こっちの方がイケてるかな。
|
61
|
+
|
62
|
+
```C++
|
63
|
+
#include <iostream> // cout, endl
|
64
|
+
#include <string> // string
|
65
|
+
#include <vector> // vector
|
66
|
+
#include <algorithm> // sort
|
67
|
+
#include <numeric> // iota
|
68
|
+
#include <random> // mt19937, random_device
|
69
|
+
#include <utility> // pair, make_pair
|
70
|
+
#include <cassert> // assert
|
71
|
+
|
72
|
+
class card {
|
73
|
+
private:
|
74
|
+
int id_;
|
75
|
+
explicit card(int id) : id_(id) {
|
76
|
+
assert( id >= 0 && id < 52 );
|
77
|
+
}
|
78
|
+
|
79
|
+
public:
|
80
|
+
card() = delete;
|
81
|
+
int suit() const { return id_ % 4; }
|
82
|
+
int rank() const { return id_ / 4; }
|
83
|
+
std::string suit_str() const { return std::string(1,"CDHS"[suit()]); }
|
84
|
+
std::string rank_str() const { return std::string(1,"A23456789TJQK"[rank()]); }
|
85
|
+
std::string str() const { return suit_str() + rank_str(); }
|
86
|
+
|
87
|
+
static card make(int id) { return card(id); }
|
88
|
+
};
|
89
|
+
|
90
|
+
inline bool operator<(const card& a, const card& b) {
|
91
|
+
return std::make_pair(a.rank(),a.suit()) < std::make_pair(b.rank(),b.suit());
|
92
|
+
}
|
93
|
+
|
94
|
+
inline std::ostream& operator<<(std::ostream& stream, const card& c) {
|
95
|
+
return stream << c.str();
|
96
|
+
}
|
97
|
+
|
98
|
+
int main() {
|
99
|
+
using namespace std;
|
100
|
+
|
101
|
+
// N人のplayer
|
102
|
+
const int N = 5;
|
103
|
+
vector<vector<card>> players(N);
|
104
|
+
|
105
|
+
// 52枚のcardからなるdeck
|
106
|
+
vector<card> deck;
|
107
|
+
for ( int id = 0; id < 52; ++id ) {
|
108
|
+
deck.emplace_back(card::make(id));
|
109
|
+
}
|
110
|
+
// deckをシャッフル
|
111
|
+
shuffle(deck.begin(), deck.end(), mt19937(random_device()()));
|
112
|
+
|
113
|
+
// deckが空になるまで、cardを各playerに配る
|
114
|
+
for ( int c = 0; !deck.empty(); ++c %= N ) {
|
115
|
+
players[c].emplace_back(deck.back());
|
116
|
+
deck.pop_back();
|
117
|
+
}
|
118
|
+
|
119
|
+
// 各playerに対し
|
120
|
+
for ( vector<card>& hand : players ) {
|
121
|
+
// 手札を(ソートして)公開
|
122
|
+
cout << hand.size() << " cards: ";
|
123
|
+
sort(hand.begin(), hand.end());
|
124
|
+
for ( const card& c : hand ) {
|
125
|
+
cout << c << ' ';
|
126
|
+
}
|
127
|
+
cout << endl;
|
128
|
+
}
|
129
|
+
}
|
58
130
|
```
|