回答編集履歴
2
指定した分割数で 1つだけ表示するコードを追加
test
CHANGED
@@ -311,3 +311,75 @@
|
|
311
311
|
{2} {1} {4} {5} {6} {3}
|
312
312
|
|
313
313
|
```
|
314
|
+
|
315
|
+
**追記2**
|
316
|
+
|
317
|
+
分割の全パターンを表示するのではなく、
|
318
|
+
|
319
|
+
指定した分割数で 1つだけ表示するようにしてみました。
|
320
|
+
|
321
|
+
```C
|
322
|
+
|
323
|
+
#include <stdio.h> // printf, putchar
|
324
|
+
|
325
|
+
#include <stdlib.h> // rand
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
#define N 12
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
void print(const int a[], int i, int j)
|
334
|
+
|
335
|
+
{
|
336
|
+
|
337
|
+
printf(" {%d", a[i]);
|
338
|
+
|
339
|
+
while (++i < j) printf(",%d", a[i]);
|
340
|
+
|
341
|
+
putchar('}');
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
void divide(const int a[], int n)
|
348
|
+
|
349
|
+
{
|
350
|
+
|
351
|
+
int i = 0;
|
352
|
+
|
353
|
+
for (int k; --n > 0; i += k) {
|
354
|
+
|
355
|
+
k = rand() % (N - i - n) + 1;
|
356
|
+
|
357
|
+
print(a, i, i + k);
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
print(a, i, N);
|
362
|
+
|
363
|
+
putchar('\n');
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
int main(void)
|
370
|
+
|
371
|
+
{
|
372
|
+
|
373
|
+
int a[N];
|
374
|
+
|
375
|
+
for (int i = 0; i < N; i++) a[i] = i + 1;
|
376
|
+
|
377
|
+
divide(a, 3);
|
378
|
+
|
379
|
+
divide(a, 2);
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
```
|
384
|
+
|
385
|
+
配列をグローバル変数ではなく、関数の引数にしました。
|
1
別解
test
CHANGED
@@ -143,3 +143,171 @@
|
|
143
143
|
{2,1,4,5,6,3}
|
144
144
|
|
145
145
|
```
|
146
|
+
|
147
|
+
**追記**
|
148
|
+
|
149
|
+
何分割するかで分類してみました。
|
150
|
+
|
151
|
+
```C
|
152
|
+
|
153
|
+
#include <stdio.h> // printf, putchar
|
154
|
+
|
155
|
+
#include <stdlib.h> // rand
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
#define N 6
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
int a[N], p[N];
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
void divide(int n)
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
if (n == 1) {
|
172
|
+
|
173
|
+
for (int i = 0; ; i++) {
|
174
|
+
|
175
|
+
int j = p[i], k = p[i+1];
|
176
|
+
|
177
|
+
printf(" {%d", a[j]);
|
178
|
+
|
179
|
+
while (++j < k) printf(",%d", a[j]);
|
180
|
+
|
181
|
+
putchar('}');
|
182
|
+
|
183
|
+
if (j == N) break;
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
putchar('\n');
|
188
|
+
|
189
|
+
return;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
for (int m = 1; ; m++) {
|
194
|
+
|
195
|
+
p[n-1] = p[n] - m;
|
196
|
+
|
197
|
+
if (p[n-1] <= 0) break;
|
198
|
+
|
199
|
+
divide(n-1);
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
int main(void)
|
208
|
+
|
209
|
+
{
|
210
|
+
|
211
|
+
for (int i = 0; i < N; i++) a[i] = i + 1;
|
212
|
+
|
213
|
+
for (int i = 0; i < N; i++) {
|
214
|
+
|
215
|
+
int r = rand() % N;
|
216
|
+
|
217
|
+
int t = a[r]; a[r] = a[i]; a[i] = t;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
for (int i = 1; i <= N; i++) {
|
222
|
+
|
223
|
+
printf("--- %d ---\n", i);
|
224
|
+
|
225
|
+
p[i] = N;
|
226
|
+
|
227
|
+
divide(i);
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
```text
|
236
|
+
|
237
|
+
--- 1 ---
|
238
|
+
|
239
|
+
{2,1,4,5,6,3}
|
240
|
+
|
241
|
+
--- 2 ---
|
242
|
+
|
243
|
+
{2,1,4,5,6} {3}
|
244
|
+
|
245
|
+
{2,1,4,5} {6,3}
|
246
|
+
|
247
|
+
{2,1,4} {5,6,3}
|
248
|
+
|
249
|
+
{2,1} {4,5,6,3}
|
250
|
+
|
251
|
+
{2} {1,4,5,6,3}
|
252
|
+
|
253
|
+
--- 3 ---
|
254
|
+
|
255
|
+
{2,1,4,5} {6} {3}
|
256
|
+
|
257
|
+
{2,1,4} {5,6} {3}
|
258
|
+
|
259
|
+
{2,1} {4,5,6} {3}
|
260
|
+
|
261
|
+
{2} {1,4,5,6} {3}
|
262
|
+
|
263
|
+
{2,1,4} {5} {6,3}
|
264
|
+
|
265
|
+
{2,1} {4,5} {6,3}
|
266
|
+
|
267
|
+
{2} {1,4,5} {6,3}
|
268
|
+
|
269
|
+
{2,1} {4} {5,6,3}
|
270
|
+
|
271
|
+
{2} {1,4} {5,6,3}
|
272
|
+
|
273
|
+
{2} {1} {4,5,6,3}
|
274
|
+
|
275
|
+
--- 4 ---
|
276
|
+
|
277
|
+
{2,1,4} {5} {6} {3}
|
278
|
+
|
279
|
+
{2,1} {4,5} {6} {3}
|
280
|
+
|
281
|
+
{2} {1,4,5} {6} {3}
|
282
|
+
|
283
|
+
{2,1} {4} {5,6} {3}
|
284
|
+
|
285
|
+
{2} {1,4} {5,6} {3}
|
286
|
+
|
287
|
+
{2} {1} {4,5,6} {3}
|
288
|
+
|
289
|
+
{2,1} {4} {5} {6,3}
|
290
|
+
|
291
|
+
{2} {1,4} {5} {6,3}
|
292
|
+
|
293
|
+
{2} {1} {4,5} {6,3}
|
294
|
+
|
295
|
+
{2} {1} {4} {5,6,3}
|
296
|
+
|
297
|
+
--- 5 ---
|
298
|
+
|
299
|
+
{2,1} {4} {5} {6} {3}
|
300
|
+
|
301
|
+
{2} {1,4} {5} {6} {3}
|
302
|
+
|
303
|
+
{2} {1} {4,5} {6} {3}
|
304
|
+
|
305
|
+
{2} {1} {4} {5,6} {3}
|
306
|
+
|
307
|
+
{2} {1} {4} {5} {6,3}
|
308
|
+
|
309
|
+
--- 6 ---
|
310
|
+
|
311
|
+
{2} {1} {4} {5} {6} {3}
|
312
|
+
|
313
|
+
```
|