回答編集履歴

2

ビット処理を使うコードを追加

2020/07/17 08:55

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -195,3 +195,81 @@
195
195
  }
196
196
 
197
197
  ```
198
+
199
+ **追記2**
200
+
201
+ こんなやり方もあります。
202
+
203
+ ```Java
204
+
205
+ class Test {
206
+
207
+ public static void main(String[] args) {
208
+
209
+ int num1[] = new int[10];
210
+
211
+ int num2[] = new int[10];
212
+
213
+
214
+
215
+ int bit1 = setRandomAndBit(num1);
216
+
217
+ int bit2 = setRandomAndBit(num2);
218
+
219
+
220
+
221
+ System.out.print("配列1:"); print(num1);
222
+
223
+ System.out.print("配列2:"); print(num2);
224
+
225
+ System.out.println("共通の数: "); print2(bit1 & bit2);
226
+
227
+ System.out.println("どちらかの数: "); print2(bit1 ^ bit2);
228
+
229
+ }
230
+
231
+
232
+
233
+ static int setRandomAndBit(int[] a) {
234
+
235
+ int bit = 0;
236
+
237
+ for (int i = 0; i < 10; i++) {
238
+
239
+ a[i] = (int) (Math.random() * 10) + 1;
240
+
241
+ bit |= 1 << a[i];
242
+
243
+ }
244
+
245
+ return bit;
246
+
247
+ }
248
+
249
+
250
+
251
+ static void print(int[] a) {
252
+
253
+ for (int i = 0; i < 10; i++)
254
+
255
+ System.out.print(" " + a[i]);
256
+
257
+ System.out.println();
258
+
259
+ }
260
+
261
+
262
+
263
+ static void print2(int bit) {
264
+
265
+ for (int i = 1; i <= 10; i++)
266
+
267
+ if ((bit >> i & 1) != 0) System.out.print(" " + i);
268
+
269
+ System.out.println();
270
+
271
+ }
272
+
273
+ }
274
+
275
+ ```

1

HashSet を使わないコードを追加

2020/07/17 08:55

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -87,3 +87,111 @@
87
87
  どちらかの数: [2, 4, 6, 8, 10]
88
88
 
89
89
  ```
90
+
91
+ **追記**
92
+
93
+ ```Java
94
+
95
+ class Test {
96
+
97
+ public static void main(String[] args) {
98
+
99
+ int num1[] = new int[10];
100
+
101
+ int num2[] = new int[10];
102
+
103
+ int same[] = new int[10];
104
+
105
+ int side[] = new int[10];
106
+
107
+
108
+
109
+ setRandom(num1);
110
+
111
+ setRandom(num2);
112
+
113
+
114
+
115
+ int n = 0, m = 0;
116
+
117
+ for (int i = 0; i < 10; i++)
118
+
119
+ if (find(num2, num1[i]))
120
+
121
+ n = add(same, n, num1[i]);
122
+
123
+ else
124
+
125
+ m = add(side, m, num1[i]);
126
+
127
+
128
+
129
+ for (int i = 0; i < 10; i++)
130
+
131
+ if (!find(num1, num2[i]))
132
+
133
+ m = add(side, m, num2[i]);
134
+
135
+
136
+
137
+ System.out.print("配列1:"); print(num1, 10);
138
+
139
+ System.out.print("配列2:"); print(num2, 10);
140
+
141
+ System.out.print("共通の数:"); print(same, n);
142
+
143
+ System.out.print("どちらかの数:"); print(side, m);
144
+
145
+ }
146
+
147
+
148
+
149
+ static void print(int[] a, int n) {
150
+
151
+ for (int i = 0; i < n; i++)
152
+
153
+ System.out.print(" " + a[i]);
154
+
155
+ System.out.println();
156
+
157
+ }
158
+
159
+
160
+
161
+ static void setRandom(int[] a) {
162
+
163
+ for (int i = 0; i < 10; i++)
164
+
165
+ a[i] = (int) (Math.random() * 10) + 1;
166
+
167
+ }
168
+
169
+
170
+
171
+ static boolean find(int[] a, int e) {
172
+
173
+ for (int i = 0; i < 10; i++)
174
+
175
+ if (a[i] == e) return true;
176
+
177
+ return false;
178
+
179
+ }
180
+
181
+
182
+
183
+ static int add(int[] a, int n, int e) {
184
+
185
+ for (int i = 0; i < n; i++)
186
+
187
+ if (a[i] == e) return n;
188
+
189
+ a[n] = e;
190
+
191
+ return n + 1;
192
+
193
+ }
194
+
195
+ }
196
+
197
+ ```