Assuming that the elements in array a [] and a are not repeated, a combination of all elements is required:
A [a, B, c]
→
→ B
→ C
→ A B
→ A C
→ B C
→ A B C
'Evaluate the enumerated results of n elements in a given array. When 'n' = 1, the number of combinations is 1, for example, a; When 'n' = 2, the number of combinations is 2, for example, AB; When 'n' = 3, the number of combinations is 3, for example, ABC; function comb (byval arr as List (of string), byval N as integer) as List (of string) dim newarray as List (of string) = new list (of string) if n = 1 then newarray = arr elseif n = 2 then for I as integer = 0 to ARR. count-1 for J as integer = I + 1 to ARR. count-1 newarray. add (ARR (I) & "&" & Arr (j) Next next else dim STR as string = "" For I as integer = 0 to ARR. count-1 StR = Arr (I) dim temparr as List (of string) = new list (of string) for J as integer = I + 1 to ARR. count-1 temparr. add (ARR (j) next if temparr. count <n-1 then exit for end if Dim arr1 as List (of string) = comb (temparr, n-1) for each strtemp as string in arr1 newarray. add (STR & "&" & strtemp) Next next end if return newarrayend Function
Usage:
Sub main () 'Use Case:' To construct an array: dim arrlist as List (of string) = new list (of string) from {"A", "B ", "C"} For I as integer = 1 to arrlist. count dim lstresult as List (of string) = comb (arrlist, I) for each STR as string in lstresult console. writeline (STR) Next next console. read () end sub
Array: "A", "B", "C", "D"
Output result:
A
B
C
D
A & B
A & C
A & D
B & C
B & D
C & D
A & B & C
A & B & D
A & C & D
B & C & D
A & B & C & D
C # version:
public static IEnumerable<string> Comb(string chars){ var data = chars.ToCharArray(); var result = new[] {""}; for(var i=0; i<chars.Length; i++) { result = (from a in data from b in result where !b.Contains(a) select a + "" + b).ToArray(); } return result;}
Input: ABCD
Output:
ABC
ACB
BAC
BCA
Cab
CBA