回答編集履歴

1

追記

2018/04/27 10:01

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -113,3 +113,147 @@
113
113
  */
114
114
 
115
115
  ```
116
+
117
+
118
+
119
+ [追記] こっちの方がイケてるかな。
120
+
121
+
122
+
123
+ ```C++
124
+
125
+ #include <iostream> // cout, endl
126
+
127
+ #include <string> // string
128
+
129
+ #include <vector> // vector
130
+
131
+ #include <algorithm> // sort
132
+
133
+ #include <numeric> // iota
134
+
135
+ #include <random> // mt19937, random_device
136
+
137
+ #include <utility> // pair, make_pair
138
+
139
+ #include <cassert> // assert
140
+
141
+
142
+
143
+ class card {
144
+
145
+ private:
146
+
147
+ int id_;
148
+
149
+ explicit card(int id) : id_(id) {
150
+
151
+ assert( id >= 0 && id < 52 );
152
+
153
+ }
154
+
155
+
156
+
157
+ public:
158
+
159
+ card() = delete;
160
+
161
+ int suit() const { return id_ % 4; }
162
+
163
+ int rank() const { return id_ / 4; }
164
+
165
+ std::string suit_str() const { return std::string(1,"CDHS"[suit()]); }
166
+
167
+ std::string rank_str() const { return std::string(1,"A23456789TJQK"[rank()]); }
168
+
169
+ std::string str() const { return suit_str() + rank_str(); }
170
+
171
+
172
+
173
+ static card make(int id) { return card(id); }
174
+
175
+ };
176
+
177
+
178
+
179
+ inline bool operator<(const card& a, const card& b) {
180
+
181
+ return std::make_pair(a.rank(),a.suit()) < std::make_pair(b.rank(),b.suit());
182
+
183
+ }
184
+
185
+
186
+
187
+ inline std::ostream& operator<<(std::ostream& stream, const card& c) {
188
+
189
+ return stream << c.str();
190
+
191
+ }
192
+
193
+
194
+
195
+ int main() {
196
+
197
+ using namespace std;
198
+
199
+
200
+
201
+ // N人のplayer
202
+
203
+ const int N = 5;
204
+
205
+ vector<vector<card>> players(N);
206
+
207
+
208
+
209
+ // 52枚のcardからなるdeck
210
+
211
+ vector<card> deck;
212
+
213
+ for ( int id = 0; id < 52; ++id ) {
214
+
215
+ deck.emplace_back(card::make(id));
216
+
217
+ }
218
+
219
+ // deckをシャッフル
220
+
221
+ shuffle(deck.begin(), deck.end(), mt19937(random_device()()));
222
+
223
+
224
+
225
+ // deckが空になるまで、cardを各playerに配る
226
+
227
+ for ( int c = 0; !deck.empty(); ++c %= N ) {
228
+
229
+ players[c].emplace_back(deck.back());
230
+
231
+ deck.pop_back();
232
+
233
+ }
234
+
235
+
236
+
237
+ // 各playerに対し
238
+
239
+ for ( vector<card>& hand : players ) {
240
+
241
+ // 手札を(ソートして)公開
242
+
243
+ cout << hand.size() << " cards: ";
244
+
245
+ sort(hand.begin(), hand.end());
246
+
247
+ for ( const card& c : hand ) {
248
+
249
+ cout << c << ' ';
250
+
251
+ }
252
+
253
+ cout << endl;
254
+
255
+ }
256
+
257
+ }
258
+
259
+ ```