回答編集履歴
2
指定した分割数で 1つだけ表示するコードを追加
answer
CHANGED
@@ -154,4 +154,40 @@
|
|
154
154
|
{2} {1} {4} {5} {6,3}
|
155
155
|
--- 6 ---
|
156
156
|
{2} {1} {4} {5} {6} {3}
|
157
|
-
```
|
157
|
+
```
|
158
|
+
**追記2**
|
159
|
+
分割の全パターンを表示するのではなく、
|
160
|
+
指定した分割数で 1つだけ表示するようにしてみました。
|
161
|
+
```C
|
162
|
+
#include <stdio.h> // printf, putchar
|
163
|
+
#include <stdlib.h> // rand
|
164
|
+
|
165
|
+
#define N 12
|
166
|
+
|
167
|
+
void print(const int a[], int i, int j)
|
168
|
+
{
|
169
|
+
printf(" {%d", a[i]);
|
170
|
+
while (++i < j) printf(",%d", a[i]);
|
171
|
+
putchar('}');
|
172
|
+
}
|
173
|
+
|
174
|
+
void divide(const int a[], int n)
|
175
|
+
{
|
176
|
+
int i = 0;
|
177
|
+
for (int k; --n > 0; i += k) {
|
178
|
+
k = rand() % (N - i - n) + 1;
|
179
|
+
print(a, i, i + k);
|
180
|
+
}
|
181
|
+
print(a, i, N);
|
182
|
+
putchar('\n');
|
183
|
+
}
|
184
|
+
|
185
|
+
int main(void)
|
186
|
+
{
|
187
|
+
int a[N];
|
188
|
+
for (int i = 0; i < N; i++) a[i] = i + 1;
|
189
|
+
divide(a, 3);
|
190
|
+
divide(a, 2);
|
191
|
+
}
|
192
|
+
```
|
193
|
+
配列をグローバル変数ではなく、関数の引数にしました。
|
1
別解
answer
CHANGED
@@ -70,4 +70,88 @@
|
|
70
70
|
{2,1,4,5} {6,3}
|
71
71
|
{2,1,4,5,6} {3}
|
72
72
|
{2,1,4,5,6,3}
|
73
|
+
```
|
74
|
+
**追記**
|
75
|
+
何分割するかで分類してみました。
|
76
|
+
```C
|
77
|
+
#include <stdio.h> // printf, putchar
|
78
|
+
#include <stdlib.h> // rand
|
79
|
+
|
80
|
+
#define N 6
|
81
|
+
|
82
|
+
int a[N], p[N];
|
83
|
+
|
84
|
+
void divide(int n)
|
85
|
+
{
|
86
|
+
if (n == 1) {
|
87
|
+
for (int i = 0; ; i++) {
|
88
|
+
int j = p[i], k = p[i+1];
|
89
|
+
printf(" {%d", a[j]);
|
90
|
+
while (++j < k) printf(",%d", a[j]);
|
91
|
+
putchar('}');
|
92
|
+
if (j == N) break;
|
93
|
+
}
|
94
|
+
putchar('\n');
|
95
|
+
return;
|
96
|
+
}
|
97
|
+
for (int m = 1; ; m++) {
|
98
|
+
p[n-1] = p[n] - m;
|
99
|
+
if (p[n-1] <= 0) break;
|
100
|
+
divide(n-1);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
int main(void)
|
105
|
+
{
|
106
|
+
for (int i = 0; i < N; i++) a[i] = i + 1;
|
107
|
+
for (int i = 0; i < N; i++) {
|
108
|
+
int r = rand() % N;
|
109
|
+
int t = a[r]; a[r] = a[i]; a[i] = t;
|
110
|
+
}
|
111
|
+
for (int i = 1; i <= N; i++) {
|
112
|
+
printf("--- %d ---\n", i);
|
113
|
+
p[i] = N;
|
114
|
+
divide(i);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
```
|
118
|
+
```text
|
119
|
+
--- 1 ---
|
120
|
+
{2,1,4,5,6,3}
|
121
|
+
--- 2 ---
|
122
|
+
{2,1,4,5,6} {3}
|
123
|
+
{2,1,4,5} {6,3}
|
124
|
+
{2,1,4} {5,6,3}
|
125
|
+
{2,1} {4,5,6,3}
|
126
|
+
{2} {1,4,5,6,3}
|
127
|
+
--- 3 ---
|
128
|
+
{2,1,4,5} {6} {3}
|
129
|
+
{2,1,4} {5,6} {3}
|
130
|
+
{2,1} {4,5,6} {3}
|
131
|
+
{2} {1,4,5,6} {3}
|
132
|
+
{2,1,4} {5} {6,3}
|
133
|
+
{2,1} {4,5} {6,3}
|
134
|
+
{2} {1,4,5} {6,3}
|
135
|
+
{2,1} {4} {5,6,3}
|
136
|
+
{2} {1,4} {5,6,3}
|
137
|
+
{2} {1} {4,5,6,3}
|
138
|
+
--- 4 ---
|
139
|
+
{2,1,4} {5} {6} {3}
|
140
|
+
{2,1} {4,5} {6} {3}
|
141
|
+
{2} {1,4,5} {6} {3}
|
142
|
+
{2,1} {4} {5,6} {3}
|
143
|
+
{2} {1,4} {5,6} {3}
|
144
|
+
{2} {1} {4,5,6} {3}
|
145
|
+
{2,1} {4} {5} {6,3}
|
146
|
+
{2} {1,4} {5} {6,3}
|
147
|
+
{2} {1} {4,5} {6,3}
|
148
|
+
{2} {1} {4} {5,6,3}
|
149
|
+
--- 5 ---
|
150
|
+
{2,1} {4} {5} {6} {3}
|
151
|
+
{2} {1,4} {5} {6} {3}
|
152
|
+
{2} {1} {4,5} {6} {3}
|
153
|
+
{2} {1} {4} {5,6} {3}
|
154
|
+
{2} {1} {4} {5} {6,3}
|
155
|
+
--- 6 ---
|
156
|
+
{2} {1} {4} {5} {6} {3}
|
73
157
|
```
|