先从数字1-20中任意选出10个数字,再对这10个数字进行分组,每组从中取出4个数字,且每组数字不能完全一样,与数字所在的位置无关。如分组1:
1,2,3,4;
2,3,4,5;
……
分组1符合条件。分组2:
1,2,3,4;
2,1,3,4;
……
分组2不符合条件,因为分组2里有两组数字完全相同。最后列出所有分组的结果,不知如何解答?
请各位高手指点。

解决方案 »

  1.   

    结果是两个组合数的积:C(20,10)*C(10,4) Dim C As Double
        
    C = CDbl(20) * 19 * 18 * 17 * 16 * 15 * 14 * 13 * 12 * 11
    C = C / (24 * 24 * 30)
    debug.print C'结果为38798760
      

  2.   

    恩,应该是10选4组合,排列就出重复数据了,试写了一个,不知道对不对:Option Explicit'获得组合总数
     Private Function Total(ByVal m As Long, ByVal n As Long) As Long
        Dim i As Long
        Dim Result As Long
        Result = 1
        For i = n To 1 Step -1
            Result = Result * m / i
            m = m - 1
        Next
        Total = Result
    End FunctionPrivate Sub Carry(Index() As Long, m As Long, n As Long)
        Dim Idx As Long
        Dim V As Long
        Idx = n
        V = m - n
        Do
            Index(Idx) = Index(Idx) + 1
            If Index(Idx) > V + Idx Then
                Idx = Idx - 1
            Else
                Exit Do
            End If
        Loop
        Do While Idx < n
            Idx = Idx + 1
            Index(Idx) = Index(Idx - 1) + 1
        Loop
    End SubPrivate Function GetResultItem(anyInput As Variant, Idx() As Long) As String
        Dim i As Long
        Dim n As Long
        n = UBound(Idx)
        ReDim arr(1 To n)
        For i = 1 To n
            arr(i) = anyInput(Idx(i) - 1)
        Next
        GetResultItem = Join(arr)
    End FunctionPrivate Sub GetResult(anyInput As Variant, ByVal n As Long, Result)
        
        Dim m As Long
        Dim Index() As Long
        Dim i As Long, j As Long
        
        m = UBound(anyInput) + 1
        ReDim Index(n)
        For i = 1 To n
            Index(i) = i
        Next
        Index(0) = -1
        
        j = Total(m, n) - 1
        ReDim Result(j)
        For i = 0 To j
            Result(i) = GetResultItem(anyInput, Index)
            Carry Index, m, n
        Next
        
    End SubPrivate Sub Command1_Click()
        '测试
        Dim a(1 To 20), b(9), Result
        Dim i As Long, r As Long
        '生成10个不重复的值放在b中
        For i = 1 To 20
            a(i) = i
        Next
        Randomize
        For i = 1 To 10
            r = Int((20 - i + 1) * Rnd + i)
            b(i - 1) = a(r)
            a(r) = a(i)
        Next
        'Debug.Print Join(b)
        '10选4生成组合,结果在Result中
        GetResult b, 4, Result
        Debug.Print Join(Result, vbCrLf)
    End Sub
      

  3.   


    Private Sub Command1_Click()
    Dim a As Variant
    a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
    Call test(a)
    End SubPrivate Sub test(DataInput As Variant)
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim l As Integer
        For i = LBound(DataInput) To UBound(DataInput) - 3
            For j = i + 1 To UBound(DataInput) - 2
                For k = j + 1 To UBound(DataInput) - 1
                    For l = k + 1 To UBound(DataInput)
                        Debug.Print DataInput(i), DataInput(j), DataInput(k), DataInput(l)
                        DoEvents
                    Next
                Next
            Next
        Next
    End Sub
    因为已经确定是4位所以用比较笨的办法,如果不定位数最好用递归的办法
      

  4.   

    改改Private Sub test(DataInput As Variant)
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim l As Integer
        For i = LBound(DataInput) To UBound(DataInput)
            For j = i To UBound(DataInput)
                For k = j To UBound(DataInput)
                    For l = k To UBound(DataInput)
                        If Not (DataInput(i) = DataInput(j) And DataInput(i) = DataInput(j) And DataInput(i) = DataInput(k) And DataInput(i) = DataInput(l)) Then
                            Debug.Print DataInput(i), DataInput(j), DataInput(k), DataInput(l)
                        End If
                        DoEvents
                    Next
                Next
            Next
        Next
    End Sub
      

  5.   

    for a=1 to 20
    for b=1 to a
    for c=1 to b
    for d=1 to c
    text1=text1+chr(10)+chr(13)& a & "," & b &"," & c & "," & dnext
    next
    next
    next