用递归打出有N个元素的集合的全部子集
如:E={a,b,c}
打出:{},{a},{b},{c},{a,b},{b,c},{a,b,c}要用递归做,大家帮个忙

解决方案 »

  1.   

    Dim combo() As StringPrivate Sub Form_Click()
    aa 6, "{a,b,c,d,e,f}"               '总的执行函数(传入值)
    End SubSub aa(n As Integer, string1 As String)
    ReDim combo(n - 1)
    For i = 2 To Len(string1)         '分解出各个元素
    tempstr = Mid(string1, i, 1)
    If tempstr = "}" Then Exit For
    If tempstr <> "," Then
    combo(j) = combo(j) & tempstr
    Else
    j = j + 1
    End If
    NextFor i = 1 To n - 1
    Print combo(i)    '打印单个元素
    Nextbb n - 1     '调用递归函数
    Print "ok"
    End SubFunction bb(n As Integer) As String   '递归函数
    If n < 0 Then Exit Function
    bb = bb(n - 1) & combo(n)
    Print bb
    End Function
      

  2.   

    我用的是VB5, 如果是VB6的话用split()函数来分解单个元素就简单多了, 呵呵
      

  3.   

    Dim combo() As String, maxnum As IntegerPrivate Sub Form_Click()
    aa 6, "{a,b,c,d,e,f}"               '总的执行函数(传入值)
    End SubSub aa(n As Integer, string1 As String)
    ReDim combo(n - 1)
    For i = 2 To Len(string1)         '分解出各个元素
    tempstr = Mid(string1, i, 1)
    If tempstr = "}" Then Exit For
    If tempstr <> "," Then
    combo(j) = combo(j) & tempstr
    Else
    j = j + 1
    End If
    Next
    maxnum = n - 1Do While maxnum >= 0
    bb n - 1              '调用递归函数
    maxnum = maxnum - 1
    LoopPrint "ok"
    End SubFunction bb(n As Integer) As String   '递归函数
    If n < maxnum Then Exit Function
    bb = bb(n - 1) & "," & combo(n)
    Print "{" & Mid(bb, 2, Len(bb) - 1) & "}"
    End Function
      

  4.   

    http://expert.csdn.net/Expert/topic/2332/2332312.xml?temp=.1893122