数组中有
1,1,2,2,3,4,5  
分别放在a(0)至a(6)位置
从这7个数字中找出 
相加后 和 等于 5 的 
或者 自身等于 5 的值

所有组合
比如
112  a(0)a(1),a(2)
112  a(0)a(1),a(3)
23   a(2),a(3)
23   a(2),a(4)
14    a(0),a(5)
14     a(1),a(5)
5      a(6) 
请问如何实现?
谢谢!!!!!!!!!

解决方案 »

  1.   

    http://topic.csdn.net/u/20070322/20/9b1e5b48-5282-4efe-bd15-d84413ea9042.html
      

  2.   


    Option Explicit
    Private Sub Carry(arr() As Long, m As Long, n As Long)   
        Dim idx As Long
        Dim V As Long
        idx = n
        V = m - n
        Do
            arr(idx) = arr(idx) + 1
            If arr(idx) > V + idx Then
                idx = idx - 1
            Else
                Exit Do
            End If
        Loop
        Do While idx < n
            idx = idx + 1
            arr(idx) = arr(idx - 1) + 1
        Loop
    End SubSub PrintArray(a, idx() As Long, mValue As String)
        Dim i As Long
        Dim sum As Long
        Dim tmp As String
        For i = 1 To UBound(idx)
            sum = sum + a(idx(i) - 1)
            tmp = tmp & a(idx(i) - 1)
        Next
        If sum = mValue Then Debug.Print tmp
    End SubPrivate Sub Command1_Click()
        Dim a()
        Dim m As Long, n As Long
        Dim i As Long, j As Long
        
        a = Array(1, 1, 2, 2, 3, 4, 5)
        m = UBound(a) + 1
        
        For i = 1 To m
            n = i
            ReDim idx(n) As Long
            For j = 1 To n
                idx(j) = j
            Next
            idx(0) = -1
        
            Do
                PrintArray a, idx, 5
                Carry idx, m, n
            Loop Until idx(0) = 0
        Next
       
    End Sub
      

  3.   

    Sub PrintArray(a, idx() As Long, mValue As String)
    中,可以改一下,笔误了:
    mValue As String ----> mValue As Long