VB. NET arrangement and combination algorithm implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.