组合N选择K

时间:2019-09-22 08:05:04

标签: vb.net

我想要什么?我想要组合N选择K。我想显示N选择K的可能组合。不幸的是,使用此代码,它与我期望的不完全相同,并且我无法使其正常工作。所以如何根据文本框使它工作?如果我选择N = 10且K = 2来显示所有可能的组合。并以更快的方式。 我的尝试:

'This your First Combination, maximal widht = element
Dim Content As String = "10"

'This Your Element
Dim Element As Integer = 2

Dim myFirstComb = (From mVal In Content.ToString.Split() Select Convert.ToInt32(mVal)).ToArray

'This Your Result List
Dim MyList As New List(Of String)

Dim myM As Integer = myFirstComb.Length
Dim myPost = myM
For myCnt = myM To 1 Step -1
    Dim LastStr = ""
    For mycnt3 = 1 To Element
        For myCnt2 = myCnt To myM
            'If myCnt2 = myM Then MsgBox("T")
            Dim myVal = myFirstComb(myCnt2 - 1)
            If myVal > Element Then myVal = 1
            If Element - myM + myCnt2 = myVal Then
                myFirstComb(myCnt2 - 1) = myVal
            Else
                If (myVal + 1) > Element Then myVal = 0
                myFirstComb(myCnt2 - 1) = myVal + 1
            End If
        Next
        Dim Hsl = String.Join(" ", myFirstComb)
        If LastStr <> Hsl Then
            MyList.Add(Hsl)
        End If
        LastStr = Hsl
    Next
    myFirstComb = (From mVal In Content.ToString.Split() Select Convert.ToInt32(mVal)).ToArray
Next

在输出中:我想要:

1,2
1,3
1,4
1,5
1,6
1,7
1,8
1,9
1,10
2,3
2,4
2,5
2,6
2,7
2,8
2,9
2,10
3,4
3,5
3,6
3,7
3,8
3,9
3,10
4,5
4,6
4,7
4,8
4,9
4,10
5,6
5,7
5,8
5,9
5,10
6,7
6,8
6,9
6,10
7,8
7,9
7,10
8,9
8,10
9,10

所以我没有处理那个代码?

0 个答案:

没有答案