2012年11月23日 星期五

大樂透中的數學1-完全包牌法

大樂透是從1到49號中,選出6個號碼為一組牌。例如,你可以選擇1,3,5,7,9,11。
從檔案中讀出玩家喜歡的號碼有10個,請輸出這10個號碼組合出所有可能的牌。
在輸出的檔案最後,輸出共有幾組牌。
輸入:in.txt
10,22,31,5,6,11,13,42,1,9

輸出:out.txt
10,22,31,5,6,11
10,22,31,5,6,13
10,22,31,5,6,42
10,22,31,5,6,1
10,22,31,5,6,9
...
共有x組牌

(題外話,完全包牌法是不可行的,下次再來試試所謂的包中3的聰明包牌法,另外還有什麼天才包牌法)

2 則留言:

  1. Dim strr, mycount
    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 = Split(strr, ",")
    Call rr("")
    Print #2, "共有" & mycount & "組牌"
    Close #2
    Close #1
    End
    End Sub

    Public Function rr(k As String)
    temp = k
    temp = Replace(temp, ",", "")
    If Len(k) - Len(temp) = 6 Then
    Print #2, Left(k, Len(k) - 1): mycount = mycount + 1
    Else
    For i = 0 To UBound(strr)
    If InStr(k, strr(i)) = 0 Then Call rr(k & strr(i) & ",")
    Next
    End If
    End Function

    回覆刪除
  2. Dim a() As String
    Dim total As Long
    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, x
    a = Split(x, ","): total = 0
    Call b("", 0)
    Print #2, "共有" & total & "組牌"
    Close #2
    Close #1
    End
    End Sub

    Function b(c, l)
    If l = 6 Then
    Print #2, Left(c, Len(c) - 1): total = total + 1
    Else
    For i = 0 To UBound(a)
    If InStr(c, a(i)) = 0 Then Call b(c & a(i) & ",", l + 1)
    Next i
    End If
    End Function

    回覆刪除