質問編集履歴

1

ありがとうございます! 
恥ずかしながら、プログラムがかなりの初心者で、HashSet.Contains() と HashSet.RemoveWhere() を使う方法が考えてみたもののわからずじまい

2018/07/16 08:50

投稿

uecky
uecky

スコア9

test CHANGED
File without changes
test CHANGED
@@ -12,9 +12,9 @@
12
12
 
13
13
 
14
14
 
15
- ```ここに言語名を入力
15
+ ```C#
16
16
 
17
- C#
17
+
18
18
 
19
19
 
20
20
 
@@ -57,3 +57,135 @@
57
57
  判定して結合していくループをどう作ったら良いのかが全くわかりません。
58
58
 
59
59
  ご教授いただければ幸いです。
60
+
61
+
62
+
63
+ ## 新たに試してみたソースコード
64
+
65
+ ```C#
66
+
67
+
68
+
69
+ //ランダムな数字を持ったハッシュセット
70
+
71
+ HashSet<int> A = new HashSet<int> { 1, 2, 6 };
72
+
73
+ HashSet<int> B = new HashSet<int> { 6, 8 };
74
+
75
+ HashSet<int> C = new HashSet<int> { 9, 5 };
76
+
77
+ HashSet<int> D = new HashSet<int> { 1, 3, 8 };
78
+
79
+ HashSet<int> E = new HashSet<int> { 7, 9 };
80
+
81
+ HashSet<int> F = new HashSet<int> { 4 };
82
+
83
+
84
+
85
+ //上記のハッシュセットをまとめたリスト
86
+
87
+ List<HashSet<int>> listA;
88
+
89
+
90
+
91
+   //最終でダブりのないハッシュセットをもつリスト
92
+
93
+ List<HashSet<int>> listB;
94
+
95
+   //補佐のリスト
96
+
97
+ List<HashSet<int>> listC;
98
+
99
+ //listBに追加するか否かのフラグ
100
+
101
+ private bool NA = false;
102
+
103
+
104
+
105
+ void Start () {
106
+
107
+ //初期化
108
+
109
+ listA = new List<HashSet<int>> { A, B, C, D, E, F };
110
+
111
+ listB = new List<HashSet<int>> { };
112
+
113
+ listC = new List<HashSet<int>> { };
114
+
115
+ }
116
+
117
+
118
+
119
+ public void Union()
120
+
121
+ {
122
+
123
+   //listAの中身を総当たりして、要素が被ってたら一番最初のハッシュセットにまとめる
124
+
125
+ for (int i = 0; i < listA.Count; i++)
126
+
127
+ {
128
+
129
+ for (int j = 1; j < listA.Count; j++)
130
+
131
+ {
132
+
133
+ if (listA[i].Overlaps(listA[j]))
134
+
135
+ {
136
+
137
+ listA[i].UnionWith(listA[j]);
138
+
139
+ }
140
+
141
+ }
142
+
143
+ NA = true;
144
+
145
+ if(listC.Count!=0){
146
+
147
+ foreach(var k in listC){
148
+
149
+ //すでに要素が被ってるものがある場合はlistBに追加しない
150
+
151
+ if (listA[i].Overlaps(k)){
152
+
153
+ NA = false;
154
+
155
+ }
156
+
157
+ }
158
+
159
+ }
160
+
161
+ if(NA==true){listB.Add(listA[i]);}
162
+
163
+ listC.Add(listA[i]);
164
+
165
+ }
166
+
167
+
168
+
169
+ print("最終チェック"+listB.Count);
170
+
171
+ foreach (var l in listB)
172
+
173
+ {
174
+
175
+ print(l + "の中の数字は");
176
+
177
+ foreach (var m in l)
178
+
179
+ {
180
+
181
+ print(m);
182
+
183
+ }
184
+
185
+ }
186
+
187
+ }
188
+
189
+
190
+
191
+ ```