2010年3月23日 星期二

保齡球計分板

在世界各地,保齡球是相當受歡迎的運動項目之一,藉著球道上滾動的球,來碰倒球道
上的球瓶,勝負以擊倒球瓶之多寡的計分來判定。請你根據下面的計分規則,寫一個保齡球
計分程式,規則如下:
一、每一局共10 格,依序完成每1 格。
二、每格的分數將累計到下1 格。
三、第1 至9 格之計分:每1 格在2 球以內,將全部10 個球瓶擊倒為原則,分數計
算方式可分為:
1. 全倒(Strike):第1 球就將全部球瓶擊倒,即完成一格。分數計算分式為10 分,
再加上下2 球的擊倒瓶數。
2. 補全倒(Spare):第1 球未全倒時,再打1 球將剩餘球瓶全部擊倒。分數計算
方式為10 分,再加下1 球的擊倒瓶數。
3. 打完第 1 球後,第2 球如未將剩餘之球瓶全部擊倒,分數為第1 球加第2 球
擊倒之球瓶數。
四、第10 格計分方法:如果前2 球為全倒或補全倒,可再加打1 球,最多打3 球。
五、計分劃記的符號代表意義如下:
1. 全倒以記號『X』來代表。
2. 補全倒以記號『/』來代表。
3. 數字代表擊倒的球數。
4. 擊倒球數為 0 時以『-』來代表。

舉例來說:
每格擊球 7- 8/ X 8- X X X X X 8/9
分數 7 27 45 53 83 113 143 171 191 210
輸入說明:
第一行的數字,表示有幾個計分板要計分,第二行開始的每一行,為一個獨立的計分板。
每一行包含10 格擊球結果。每格以一個空白作為區隔。
輸出說明:
對輸入的每個計分板,分別計算出後的總分數。
輸入範例:
2
7- 8/ X 8- X X X X X 8/9
X X X X X X X X X XXX
輸出範例:
210
300

4 則留言:

  1. Private Sub Form_Load()
    Dim a(30), b(30)
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For i = 1 To n
    Line Input #1, x
    x = Replace(x, " ", "")
    'Print x
    'List1.AddItem x
    For j = 1 To Len(x)
    a(j) = Mid(x, j, 1)
    'Print j, a(j)
    'List1.AddItem j & " " & a(j)
    Next j

    '開始計分
    For j = 1 To Len(x)
    If a(j) = "X" Then b(j) = 10
    If a(j) = "-" Then b(j) = 0
    If a(j) >= 1 And a(j) <= 9 Then b(j) = a(j)
    If a(j) = "/" Then b(j) = 10 - b(j - 1)
    Next j

    For j = 1 To Len(x)
    Select Case a(j)
    'Case 1 To 9:
    'b(j) = Val(a(j))
    'Case "-":
    'b(j) = 0
    Case "/":
    b(j) = b(j) + b(j + 1)
    Case "X":
    b(j) = 10 + b(j + 1) + b(j + 2)
    End Select
    'Print j, b(j)
    'List2.AddItem j & " " & b(j)
    Next j
    If a(Len(x) - 1) = "/" Then
    Max = Len(x) - 1
    ElseIf a(Len(x) - 2) = "X" Then
    Max = Len(x) - 2
    Else
    Max = Len(x)
    End If
    Sum = 0
    For j = 1 To Max
    Sum = Sum + b(j)
    Next j
    Print #2, Sum
    'List2.AddItem Sum
    Next i
    Close #1, #2
    End Sub
    '-------------------------------
    in.txt:
    4
    7- 8/ X 8- X X X X X 8/9
    X X X X X X X X X XXX
    9- 52 X X 5- 5/ 62 35 62 9-
    XXXXXXXXX9/X

    out.txt:
    210
    300
    110
    279

    回覆刪除
  2. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, s
    For Star = 1 To s
    Line Input #1, x
    ReDim class(10) As Integer, ball(21) As Integer, check(21) As Integer
    a = Split(x, " ")
    txt = "-123456789X"
    ans = 0
    n = 1
    For i = 0 To UBound(a)
    For j = 1 To Len(a(i))
    k = Mid(a(i), j, 1)
    If k = "/" Then
    class(i) = class(i) + (11 - InStr(txt, Mid(a(i), j - 1, 1)))
    ball(n) = (11 - InStr(txt, Mid(a(i), j - 1, 1)))
    check(n) = 1
    Else
    class(i) = class(i) + InStr(txt, k) - 1
    ball(n) = InStr(txt, k) - 1
    End If
    n = n + 1
    Next j
    Next i
    n = n - 1
    For i = 1 To n - Len(a(9))
    If ball(i) = 10 And check(i) = 0 Then
    ans = ans + 10 + ball(i + 1) + ball(i + 2)
    ElseIf check(i) = 1 Then
    ans = ans + 10 + ball(i + 1)
    ElseIf ball(i) >= 1 And ball(i) < 10 And check(i + 1) = 0 Then
    ans = ans + ball(i)
    End If
    Next i
    Print #2, ans + class(9)
    Next Star
    Close #2
    Close #1
    End Sub

    回覆刪除
  3. Dim P(22) As Integer, aa(22) As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, qqq
    For i = 1 To qqq
    Line Input #1, a
    ans = 0
    For j = 0 To c
    aa(j) = ""
    P(j) = 0
    Next j
    b = Split(a, " ")
    c = 1
    For j = 0 To UBound(b)
    For s = 1 To Len(b(j))
    aa(c) = Mid(b(j), s, 1)
    c = c + 1
    Next s
    Next j
    c = c - 1
    If aa(c) = "X" Then P(c) = 10
    If aa(c) = "X" And aa(c - 1) = "X" Then ans = ans - 20
    For j = c To 1 Step -1
    If Val(aa(j)) > 0 And Val(aa(j)) <= 9 Then
    P(j) = Val(aa(j))
    ans = ans + P(j)
    End If
    Next j
    For j = c To 1 Step -1
    Select Case aa(j)
    Case "/"
    P(j) = 10 - P(j - 1)
    ans = ans + P(j) + P(j + 1)
    Case "X"
    P(j) = 10
    ans = ans + P(j) + P(j + 1) + P(j + 2)
    Case "-"
    P(j) = 0
    End Select
    Next j
    ans = ans - P(c)
    Print #2, ans
    Next i
    Close #2
    Close #1
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Dim w(21) As Integer
    Dim s(21)
    Dim f(10) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, q
    For i = 1 To q
    Line Input #1, k
    k = Replace(k, " ", "")
    p = 1
    z = 1
    Do Until k = ""
    s(z) = Left(k, 1)
    k = Mid(k, 2, Len(k) - 1)
    Select Case s(z)
    Case "/"
    w(p) = 10 - w(p - 1)
    Case "-"
    w(p) = 0
    Case "X"
    w(p) = 10
    Case Else
    w(p) = Val(s(z))
    End Select
    p = p + 1
    z = z + 1
    Loop
    Print
    '---------------------------------
    For j = 1 To p - 3
    Select Case s(j)
    Case "/"
    w(j) = w(j) + w(j + 1)
    Case "X"
    w(j) = w(j) + w(j + 1) + w(j + 2)
    End Select
    ans = ans + w(j)

    Next j
    If s(p - 2) = "/" Then ans = ans + w(p - 1) + w(p - 2)
    Print #2, ans
    For j = 1 To 21
    w(j) = 0
    Next j

    ans = 0
    Next i
    Close #2
    Close #1
    End Sub

    回覆刪除