2012年11月23日 星期五

子集合

輸入一變數N,再輸入N 個數字成為一個含有N 個數的集合A, 


然後輸出所有這個A 集合的子集合。 

Ex: N=3, A { 1, 2, 3 } 

ANS: { } <--- 空集合 

{ 1 },{ 2 },{ 3 } 

{ 1, 2 },{ 1, 3 },{ 2, 3 } 

{ 1, 2, 3 }

2 則留言:

  1. Dim strr
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, strr
    strr = Right(strr, Len(strr) - InStr(strr, "{"))
    strr = Replace(strr, "}", "")
    strr = Replace(strr, " ", "")
    strr = Split(strr, ",")
    Call rr("", 0)
    Close #2
    Close #1
    End
    End Sub

    Public Function rr(k As String, ByVal x As Integer)
    temp = "{" & k & "}"
    temp = Replace(temp, ",}", "}")
    Print #2, temp
    For i = x To UBound(strr)
    If InStr(k, strr(i)) = 0 Then Call rr(k & strr(i) & ",", i)
    Next

    End Function

    回覆刪除
  2. Dim a() As String
    Dim n As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, g
    Line Input #1, x
    g = Replace(g, "N=", "")
    x = LTrim(Replace(x, "A { ", ""))
    x = Replace(x, " } ", "")
    a = Split(x, ", ")
    n = Val(g)
    Print #2, "{ }"
    For i = 1 To n
    Call b("", 0, i)
    Next i
    Close #2
    Close #1
    End
    End Sub

    Sub b(c, d, t)
    If Len(c) = t * 2 Then
    c = "{ " & c & " }"
    c = Replace(c, ", }", " }")
    Print #2, c
    Else
    For i = d To UBound(a)
    If InStr(c, a(i)) = 0 Then Call b(c & a(i) & ",", i + 1, t)
    Next i
    End If
    End Sub

    'in.txt
    N=3, A { 1, 2, 3 }
    'out.txt
    { }
    { 1 }
    { 2 }
    { 3 }
    { 1,2 }
    { 1,3 }
    { 2,3 }
    { 1,2,3 }

    回覆刪除