回答編集履歴
7
バグ修正
test
CHANGED
@@ -218,9 +218,13 @@
|
|
218
218
|
|
219
219
|
}
|
220
220
|
|
221
|
+
for (; i < N; i++)
|
222
|
+
|
221
|
-
|
223
|
+
if (a1[i] != t) b[k++] = t = a1[i];
|
224
|
+
|
222
|
-
|
225
|
+
for (; j < N; j++)
|
226
|
+
|
223
|
-
|
227
|
+
if (a2[j] != t) b[k++] = t = a2[j];
|
224
228
|
|
225
229
|
return k;
|
226
230
|
|
6
コードの改善
test
CHANGED
@@ -212,16 +212,12 @@
|
|
212
212
|
|
213
213
|
else {
|
214
214
|
|
215
|
-
if (a1[i] == a2[j]) i++;
|
216
|
-
|
217
215
|
if (a2[j] != t) b[k++] = t = a2[j];
|
218
216
|
|
219
217
|
j++;
|
220
218
|
|
221
219
|
}
|
222
220
|
|
223
|
-
|
224
|
-
|
225
221
|
while (i < N) b[k++] = a1[i++];
|
226
222
|
|
227
223
|
while (j < N) b[k++] = a2[j++];
|
5
コードの改善
test
CHANGED
@@ -210,7 +210,9 @@
|
|
210
210
|
|
211
211
|
}
|
212
212
|
|
213
|
+
else {
|
214
|
+
|
213
|
-
|
215
|
+
if (a1[i] == a2[j]) i++;
|
214
216
|
|
215
217
|
if (a2[j] != t) b[k++] = t = a2[j];
|
216
218
|
|
@@ -218,13 +220,7 @@
|
|
218
220
|
|
219
221
|
}
|
220
222
|
|
221
|
-
|
223
|
+
|
222
|
-
|
223
|
-
if (a1[i] != t) b[k++] = t = a1[i];
|
224
|
-
|
225
|
-
i++, j++;
|
226
|
-
|
227
|
-
}
|
228
224
|
|
229
225
|
while (i < N) b[k++] = a1[i++];
|
230
226
|
|
4
ソートを使うコードを追加
test
CHANGED
@@ -111,3 +111,179 @@
|
|
111
111
|
ビット演算を使わなければ理解できるのでしょうか?
|
112
112
|
|
113
113
|
コメントをお願いします。
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
**追記2**
|
118
|
+
|
119
|
+
ソートした 2つの配列を先頭から見ていって、積集合と和集合を求めるやり方。
|
120
|
+
|
121
|
+
```C
|
122
|
+
|
123
|
+
#include <stdio.h>
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
#define N 10 // 配列の要素数
|
128
|
+
|
129
|
+
#define A 1 // 要素の値の範囲 A以上 B以下
|
130
|
+
|
131
|
+
#define B 10
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
void print(int a[], int n)
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
for (int i = 0; i < n; i++)
|
140
|
+
|
141
|
+
printf(" %d", a[i]);
|
142
|
+
|
143
|
+
putchar('\n');
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
void sort(int a[])
|
150
|
+
|
151
|
+
{
|
152
|
+
|
153
|
+
for (int i = 0; i < N-1; i++)
|
154
|
+
|
155
|
+
for (int j = i+1; j < N; j++)
|
156
|
+
|
157
|
+
if (a[i] > a[j]) {
|
158
|
+
|
159
|
+
int t = a[i]; a[i] = a[j]; a[j] = t;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
int intersection(int a1[], int a2[], int b[])
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
int i = 0, j = 0, k = 0, t = A-1;
|
172
|
+
|
173
|
+
while (i < N && j < N)
|
174
|
+
|
175
|
+
if (a1[i] < a2[j])
|
176
|
+
|
177
|
+
i++;
|
178
|
+
|
179
|
+
else if (a1[i] > a2[j])
|
180
|
+
|
181
|
+
j++;
|
182
|
+
|
183
|
+
else {
|
184
|
+
|
185
|
+
if (a1[i] != t) b[k++] = t = a1[i];
|
186
|
+
|
187
|
+
i++, j++;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
return k;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
int union_set(int a1[], int a2[], int b[])
|
198
|
+
|
199
|
+
{
|
200
|
+
|
201
|
+
int i = 0, j = 0, k = 0, t = A-1;
|
202
|
+
|
203
|
+
while (i < N && j < N)
|
204
|
+
|
205
|
+
if (a1[i] < a2[j]) {
|
206
|
+
|
207
|
+
if (a1[i] != t) b[k++] = t = a1[i];
|
208
|
+
|
209
|
+
i++;
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
else if (a1[i] > a2[j]) {
|
214
|
+
|
215
|
+
if (a2[j] != t) b[k++] = t = a2[j];
|
216
|
+
|
217
|
+
j++;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
else {
|
222
|
+
|
223
|
+
if (a1[i] != t) b[k++] = t = a1[i];
|
224
|
+
|
225
|
+
i++, j++;
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
while (i < N) b[k++] = a1[i++];
|
230
|
+
|
231
|
+
while (j < N) b[k++] = a2[j++];
|
232
|
+
|
233
|
+
return k;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
int main(void)
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
int a1[N] = { 5, 8, 4, 3, 7, 9, 7, 9, 5, 1 };
|
244
|
+
|
245
|
+
int a2[N] = { 7, 6, 1, 5, 9, 9, 10, 8, 4, 3 };
|
246
|
+
|
247
|
+
printf("配列1:"), print(a1, N);
|
248
|
+
|
249
|
+
printf("配列2:"), print(a2, N);
|
250
|
+
|
251
|
+
sort(a1);
|
252
|
+
|
253
|
+
printf("配列1(昇順):"), print(a1, N);
|
254
|
+
|
255
|
+
sort(a2);
|
256
|
+
|
257
|
+
printf("配列2(昇順):"), print(a2, N);
|
258
|
+
|
259
|
+
int b[B-A+1], n;
|
260
|
+
|
261
|
+
n = intersection(a1, a2, b);
|
262
|
+
|
263
|
+
printf("共通する数:"), print(b, n);
|
264
|
+
|
265
|
+
n = union_set(a1, a2, b);
|
266
|
+
|
267
|
+
printf("どちらかにある数:"), print(b, n);
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
```
|
272
|
+
|
273
|
+
実行結果
|
274
|
+
|
275
|
+
```text
|
276
|
+
|
277
|
+
配列1: 5 8 4 3 7 9 7 9 5 1
|
278
|
+
|
279
|
+
配列2: 7 6 1 5 9 9 10 8 4 3
|
280
|
+
|
281
|
+
配列1(昇順): 1 3 4 5 5 7 7 8 9 9
|
282
|
+
|
283
|
+
配列2(昇順): 1 3 4 5 6 7 8 9 9 10
|
284
|
+
|
285
|
+
共通する数: 1 3 4 5 7 8 9
|
286
|
+
|
287
|
+
どちらかにある数: 1 3 4 5 6 7 8 9 10
|
288
|
+
|
289
|
+
```
|
3
コードの修正
test
CHANGED
@@ -88,7 +88,7 @@
|
|
88
88
|
|
89
89
|
int a2[N] = { 7, 6, 1, 5, 9, 9, 10, 8, 4, 3 };
|
90
90
|
|
91
|
-
int b1[B] = { 0 }, b2[B] = { 0 }, c[B+1] = { 0 }, d[B+1] = { 0 };
|
91
|
+
int b1[B+1] = { 0 }, b2[B+1] = { 0 }, c[B+1] = { 0 }, d[B+1] = { 0 };
|
92
92
|
|
93
93
|
for (int i = 0; i < N; i++) b1[a1[i]] = b2[a2[i]] = 1;
|
94
94
|
|
2
ビット演算を使わないコードを追加
test
CHANGED
@@ -47,3 +47,67 @@
|
|
47
47
|
}
|
48
48
|
|
49
49
|
```
|
50
|
+
|
51
|
+
**追記**
|
52
|
+
|
53
|
+
ビット演算を使わないやり方です。
|
54
|
+
|
55
|
+
```C
|
56
|
+
|
57
|
+
#include <stdio.h>
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
#define N 10 // 配列の要素数
|
62
|
+
|
63
|
+
#define A 1 // 要素の値の範囲 A以上 B以下
|
64
|
+
|
65
|
+
#define B 10
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
void print(int b[B+1])
|
70
|
+
|
71
|
+
{
|
72
|
+
|
73
|
+
for (int i = A; i <= B; i++)
|
74
|
+
|
75
|
+
if (b[i]) printf(" %d", i);
|
76
|
+
|
77
|
+
putchar('\n');
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
int main(void)
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
int a1[N] = { 5, 8, 4, 3, 7, 9, 7, 9, 5, 1 };
|
88
|
+
|
89
|
+
int a2[N] = { 7, 6, 1, 5, 9, 9, 10, 8, 4, 3 };
|
90
|
+
|
91
|
+
int b1[B] = { 0 }, b2[B] = { 0 }, c[B+1] = { 0 }, d[B+1] = { 0 };
|
92
|
+
|
93
|
+
for (int i = 0; i < N; i++) b1[a1[i]] = b2[a2[i]] = 1;
|
94
|
+
|
95
|
+
for (int i = A; i <= B; i++) {
|
96
|
+
|
97
|
+
c[i] = b1[i] * b2[i]; // 積集合
|
98
|
+
|
99
|
+
d[i] = b1[i] + b2[i]; // 和集合
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
printf("共通する数:"), print(c);
|
104
|
+
|
105
|
+
printf("どちらかにある数:"), print(d);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
```
|
110
|
+
|
111
|
+
ビット演算を使わなければ理解できるのでしょうか?
|
112
|
+
|
113
|
+
コメントをお願いします。
|
1
10の意味の違いが分かるようにコードを修正
test
CHANGED
@@ -6,11 +6,19 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
#define N 10 // 配列の要素数
|
10
|
+
|
11
|
+
#define A 1 // 要素の値の範囲 A以上 B以下
|
12
|
+
|
13
|
+
#define B 10
|
14
|
+
|
15
|
+
|
16
|
+
|
9
17
|
void print(int b)
|
10
18
|
|
11
19
|
{
|
12
20
|
|
13
|
-
for (int i =
|
21
|
+
for (int i = A; i <= B; i++)
|
14
22
|
|
15
23
|
if (b >> i & 1) printf(" %d", i);
|
16
24
|
|
@@ -24,13 +32,13 @@
|
|
24
32
|
|
25
33
|
{
|
26
34
|
|
27
|
-
int a1[] = { 5, 8, 4, 3, 7, 9, 7, 9, 5, 1 };
|
35
|
+
int a1[N] = { 5, 8, 4, 3, 7, 9, 7, 9, 5, 1 };
|
28
36
|
|
29
|
-
int a2[] = { 7, 6, 1, 5, 9, 9, 10, 8, 4, 3 };
|
37
|
+
int a2[N] = { 7, 6, 1, 5, 9, 9, 10, 8, 4, 3 };
|
30
38
|
|
31
39
|
int b1 = 0, b2 = 0;
|
32
40
|
|
33
|
-
for (int i = 0; i <
|
41
|
+
for (int i = 0; i < N; i++) b1 |= 1 << a1[i], b2 |= 1 << a2[i];
|
34
42
|
|
35
43
|
printf("共通する数:"), print(b1 & b2);
|
36
44
|
|