2012年9月23日 星期日

100正式Problem:4-2

小朋友常喜歡玩很多撲克牌的遊戲。撲克牌有四種花色,黑桃、紅桃、方塊、和梅花。
「同花順」為同花色五張連續數字,相同花色的「順子」。
「四條」為四張同數字的牌,外加任一單張的五張牌;
「葫蘆」為三張同數字,另兩張同數字的牌;一個「一對」和「三條」所組成的五張牌;
「順子」為五張數字連續的牌,數字各差1點的連續牌,從最小 A-2-3-4-5(1-2-3-4-5),到最
最大10-J-Q-K-A(10-11-12-13-14),但不包括J-Q-K-A-2 的連續方式。
「三條」為三張同數字;
「兩對」是有兩對兩兩同數字的牌;
「一對」則是只有兩張同數字;
「雜牌」指不屬於以上任何一種組合。
五張牌依照牌面可能有多種組合,一般判斷大小的順序如下:
同花順 > 四條 > 葫蘆 > 順子 > 三條 > 兩對 > 一對 > 雜牌
同樣組合時,先比數字大小(A 最大再來是K(13)大,2 最小),再比花色。每張牌以一個字母表示花色(S 表黑桃,H 表紅桃,D 表方塊,C 表梅花)及一個介於1~13 之間的數字(A:1、J:11、Q:12、K:13)。花色大小順序為:
黑桃 > 紅桃 > 方塊 > 梅花
例如黑桃同花順 9、10、J(11)、Q(12)、K(13)小於紅桃同花順10、J(11)、Q(12)、K(13)、A,
但大於方塊同花順9、10、J(11)、Q(12)、K(13)。葫蘆以三條的大小作判斷,也就是說2、2、
10、10、10 大於8、8、9、9、9。兩對則以較大的對作判斷,同樣數字時,有黑桃的人贏。
例如:H1 D6 C6 H12 S12 大於H2 D9 C9 D12 C12,因為S12 為黑桃12。雜牌以其中最大的牌作判斷。請幫小朋友們寫個程式,判斷那個小朋友手上的五張牌,找出誰的牌最大和最小。
輸入說明:
輸入資料含多個測試案例,每個檔案 in1.txt 和in2.txt 都在各有一副牌52 張的情況下,第1行的數字n 代表有幾筆資料要測試,而n 的值介於3(含)和8(含)之間,每個案例有五張牌,每個測試案例,是其中一位小朋友手上的牌。
輸出說明:
按照小朋友手上的五張牌,在 n 個測試案例中,找出那個小朋友的牌是最大和最小,結果按照原來牌的順序輸出那二位小朋友牌是最大和最小的五張牌。每個檔案in1.txt 和in2.txt,各找出一組最大和最小的五張牌。

輸入範例:【檔名:in1.txt】
4
S3 H5 S4 D5 C5
H7 H8 H10 H9 H11
H1 D6 C6 H12 S12
D12 C12 D9 C9 S13
輸入範例:【檔名:in2.txt】
4
D2 H5 S2 D5 C5
D3 H4 S1 D7 C8
H1 S7 C7 H13 S13
C2 S3 S4 S6 S8
輸出範例:【檔名:out.txt】
H7 H8 H10 H9 H11
D12 C12 D9 C9 S13
D2 H5 S2 D5 C5
D3 H4 S1 D7 C8

3 則留言:

  1. Dim b, x, k, j
    Dim h As Boolean
    Private Sub Form_Load()
    Me.Hide
    Dim c As String
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\in2.txt" For Input As #2
    Open App.Path & "\out.txt" For Output As #3
    Input #1, n
    Input #2, n1
    For i = 1 To n
    List1.Clear
    Line Input #1, x
    b = Split(x, " ")
    Call abc("")
    Next
    Call abc2
    Print #3,
    For i = 1 To n1
    Line Input #2, x
    b = Split(x, " ")
    Call abc("")
    Next
    Call abc2
    Close
    Close
    Close
    End
    End Sub
    Sub abc(t)
    h = False
    For i = 0 To UBound(b)
    If Len(b(i)) = 3 Then
    List1.AddItem Val(Right(b(i), 2)) + 100 & " " & Left(b(i), 1)
    Else
    List1.AddItem Val(Right(b(i), 1)) + 100 & " " & Left(b(i), 1)
    If Right(b(i), 1) = 1 Then List1.AddItem "114" & " " & Left(b(i), 1)
    End If
    Next
    s = 1
    s1 = 1
    For i = 0 To List1.ListCount - 2
    c = Split(List1.List(i), " ")
    d = Split(List1.List(i + 1), " ")
    If Val(d(0)) = Val((c(0) + 1)) Then s = s + 1: h = True
    If c(1) = d(1) Then s1 = s1 + 1
    Next
    If s = 5 And s1 = 5 Then List2.AddItem "1" & " " & x & " " & List1.List(List1.ListCount - 1)
    If s = 5 And s1 <> 5 Then List2.AddItem "4" & " " & x & " " & List1.List(List1.ListCount - 1)
    s = 1: s2 = 0: s3 = 0: s4 = 0
    For i = 0 To List1.ListCount - 2
    c = Split(List1.List(i), " ")
    d = Split(List1.List(i + 1), " ")
    If Val(c(0)) = Val(d(0)) Then s = s + 1: h = True
    If c(0) <> d(0) And s = 2 Then s2 = s2 + 1: s = 1: Max2 = c(0): h = True
    If c(0) <> d(0) And s = 3 Then s3 = s3 + 1: s = 1: Max3 = c(0): h = True
    If i = (List1.ListCount - 2) Then
    If s = 2 Then s2 = s2 + 1: Max2 = c(0)
    If s = 3 Then s3 = s3 + 1: Max3 = c(0)
    End If
    If s = 4 Then s4 = s4 + 1
    Next
    If s4 = 1 Then List2.AddItem "2" & " " & x & " " & c(0):
    If s3 = 1 And s2 = 1 Then List2.AddItem "3" & " " & x & " " & Max3
    If s3 = 1 And s2 <> 1 Then List2.AddItem "5" & " " & x & " " & Max3
    If s2 = 2 Then List2.AddItem "6" & " " & x & " " & Max2
    If s2 = 1 And s3 <> 1 Then List2.AddItem "7" & " " & x & " " & Max2
    If h = False Then List2.AddItem "8" & " " & x & List1.List(List1.ListCount - 1)
    End Sub

    回覆刪除
  2. Sub abc1()
    For i = 0 To List2.ListCount - 2
    k = Split(List2.List(i), " ")
    j = Split(List2.List(i + 1), " ")
    If k(0) = j(0) Then
    If k(0) = 1 Then Call abc3(i)
    If k(6) > j(6) Then
    List2.List(i) = "0 0"
    ElseIf k(6) < j(6) Then
    List2.List(i + 1) = "0 0"
    Else
    If k(0) = 4 Then
    Call abc4(i)
    Else
    For g = 1 To 5
    If Str(Mid(k(g), 2)) = Str(k(6) - 100) And Mid(k(g), 1, 1) = "S" Then List2.List(i) = "0 0": Exit For
    If Str(Mid(j(g), 2)) = Str(j(6) - 100) And Mid(j(g), 1, 1) = "S" Then List2.List(i + 1) = "0 0": Exit For
    Next
    End If
    End If
    End If
    Next
    End Sub
    Sub abc3(i)
    If Mid(k(1), 1, 1) = "S" Then
    List2.List(i) = "0 0"
    ElseIf Mid(j(1), 1, 1) = "S" Then List2.List(i) = "0 0"
    ElseIf Mid(j(1), 1, 1) = "H" Then List2.List(i) = "0 0"
    ElseIf Mid(k(1), 1, 1) = "H" Then List2.List(i) = "0 0"
    ElseIf Mid(j(1), 1, 1) = "D" Then List2.List(i) = "0 0"
    ElseIf Mid(k(1), 1, 1) = "D" Then List2.List(i) = "0 0"
    End If
    End Sub
    Sub abc4(i)
    If Mid(k(5), 1, 1) = "S" Then
    List2.List(i) = "0 0"
    ElseIf Mid(j(5), 1, 1) = "S" Then List2.List(i) = "0 0"
    ElseIf Mid(j(5), 1, 1) = "H" Then List2.List(i) = "0 0"
    ElseIf Mid(k(5), 1, 1) = "H" Then List2.List(i) = "0 0"
    ElseIf Mid(j(5), 1, 1) = "D" Then List2.List(i) = "0 0"
    ElseIf Mid(k(5), 1, 1) = "D" Then List2.List(i) = "0 0"
    End If
    End Sub
    Sub abc2()
    For i = 1 To List2.ListCount - 2
    k = Split(List2.List(i), " ")
    l = Split(List2.List(i - 1), " ")
    j = Split(List2.List(i + 1), " ")
    If k(0) > l(0) And k(0) < j(0) Then List2.List(i) = "0 0"
    Next
    Call abc1
    For i = 0 To List2.ListCount - 1
    If List2.List(i) = "0 0" Then List2.RemoveItem i
    Next
    For i = 0 To List2.ListCount - 1
    a1 = Split(List2.List(i), " ")
    If a1(0) <> 0 Then
    For j = 1 To 5
    Print #3, a1(j) & " ";
    Next
    End If
    Print #3,
    Next
    List1.Clear
    List2.Clear
    End Sub

    回覆刪除
  3. 寫好久,需要的判斷好多,且程式碼太長,無法一次PO上來,所以分兩次PO,程式還需要改進!

    回覆刪除