2011年6月20日 星期一

題1 大樂透中的數學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的聰明包牌法,另外還有什麼天才包牌法)

4 則留言:

  1. Dim s(9)
    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, s(0), s(1), s(2), s(3), s(4), s(5), s(6), s(7), s(8), s(9)

    Call Q("", 0)

    Close
    Close
    End
    End Sub

    Sub Q(ans As String, dot)
    If dot = 6 Then
    Print #2, Left(ans, Len(ans) - 1)
    Else
    For i = 0 To 9
    If InStr(ans, s(i)) = 0 Then Call Q(ans & s(i) & ",", dot + 1)
    Next
    End If
    End Sub

    回覆刪除
  2. 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, N
    Y = Split(N, ",")
    For i = 0 To UBound(Y)
    X(i) = Y(i)
    z(i) = 0
    Next i
    ans = 0
    Call abc("", 0)
    Print #2, "共有" & ans & "組牌"
    Close #2
    Close #1
    End
    End Sub

    Sub abc(ByVal A, ByVal B)
    If B = 6 Then
    Print #2, Right(A, Len(A) - 1)
    ans = ans + 1
    Else
    For i = 0 To UBound(X)
    If NOre(X(i)) = fasle Then z(B) = X(i): Call abc(A & "," & X(i), B + 1)
    Next i
    End If
    End Sub

    Function NOre(N)
    Dim re As Boolean
    re = False
    For i = 0 To 5
    If z(i) = N Then re = True: NOre = True: Exit For
    Next i
    If re = False Then NOre = False
    End Function

    回覆刪除
  3. Dim m(10)
    Private Sub Form_Load()
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    For i = 0 To 9
    Input #1, m(i)
    Next
    'Call c("", m, 0)
    For i = 0 To 9
    a = m(i) & a
    For ii = 0 To 9
    If m(ii) <> m(i) Then a = m(ii) & a
    For iii = 0 To 9
    If m(iii) <> m(i) And m(iii) <> m(ii) Then a = m(iii) & a
    For iiii = 0 To 9
    If m(iiii) <> m(i) And m(iiii) <> m(ii) And m(iiii) <> m(iii) Then a = m(iiii) & a
    For iiiii = 0 To 9
    If m(iiiii) <> m(i) And m(iiiii) <> m(ii) And m(iiiii) <> m(iii) And m(iiiii) <> m(iiii) Then a = m(iiiii) & a
    For iiiiii = 0 To 9
    If m(iiiiii) <> m(i) And m(iiiiii) <> m(ii) And m(iiiiii) <> m(iii) And m(iiiiii) <> m(iiii) And m(iiiiii) <> m(iiiii) Then a = m(iiiiii) & a: Print #2, a, c = c + 1
    Next
    Next
    Next
    Next
    Next
    Next
    Print #2, "共有 " & c & " 組牌"
    Close #1
    Close #2
    End Sub
    Sub c(a, b, j)
    If j = 6 Then
    j = 0
    Print #2, a
    Else
    For i = 0 To UBound(b)
    If InStr(a, b(i)) = 0 Then j = j + 1: Call c(a & b(i) & ",", b, j)
    Next
    End If
    End Sub

    回覆刪除
  4. arro:少了共幾組。
    佑、緣尉:正確。
    三人的程式,雖然以arro的程式最好,但是,0分。要小心看題目。

    回覆刪除