2012年10月25日 星期四

VB101正副選手考2 (2012.10.24考試)

VB101正副選手考2 (2012.10.24考試)
1.數獨
「數獨sudoku」來自日文,但概念源自「拉丁方塊」,是十八世紀瑞士數學家歐拉發明的。其遊戲規則為: 在九個九宮格裡,填入1到9的數字,讓每個數字在每個行、列及九宮格裡都只出現一次。謎題中會預先填入若干數字,其他宮位則留白,玩家得依謎題中的數字分佈狀況,邏輯推敲出剩下的空格裡是什麼數字,範例如下圖所示。請設計一個程式,可以幫助玩家提示空格中尚可填入的數字。例如:第5行第3列的位置尚可填入1這個數字;第6行第7列的位置尚可填入7與8這兩個數字;而第5行第5列的位置已有數字填入則輸出0。

輸入說明:輸入的第一列到第九列為數獨資料,每一列由九個數字所組成。數字1到9為已填入的數值,0代表尚未填入數值的空格,每個數字與數字間的區隔為一個空白符號。第十列及以後的資料為要提示位置的座標(座標的定義方式為右上角為9 1,左下角為1 9,第一個數字代表行,第二個數字代表列),當為0 0時表示結束。(請參照輸入範例)

輸入範例:test3.txt

4 0 0 8 0 5 0 1 0
2 0 0 0 0 9 0 8 0
6 0 8 0 0 3 4 9 0
0 0 5 0 2 0 6 0 0
0 0 1 0 7 0 9 0 0
0 0 7 0 8 0 3 0 0
0 9 0 1 0 0 0 0 2
0 8 0 4 0 0 0 0 9
0 3 0 6 0 2 0 0 7
5 3
6 7
5 5
0 0

輸出說明:每個提示位置有一列輸出結果,表示尚可填入的數字,請由小到大排列。每個數字與數字間的區隔為一個空白符號。若該位置已有填入數字,或找不到提示的數字則輸出0。(請參照輸出範例)
輸出範例:result3.txt
1
7 8
0


2.保齡球計分板

在世界各地,保齡球是相當受歡迎的運動項目之一,藉著球道上滾動的球,來碰倒球道上的球瓶,勝負以擊倒球瓶之多寡的計分來判定。請你根據下面的計分規則,寫一個保齡球
計分程式,規則如下:
一、每一局共10 格,依序完成每1 格。
二、每格的分數將累計到下1 格。
三、第1 至9 格之計分:每1 格在2 球以內,將全部10 個球瓶擊倒為原則,分數計算方式可分為:
一、 全倒(Strike):第1 球就將全部球瓶擊倒,即完成一格。分數計算分式為10 分,再加上下2 球的擊倒瓶數。
二、補全倒(Spare):第1 球未全倒時,再打1 球將剩餘球瓶全部擊倒。分數計算方式為10 分,再加下1 球的擊倒瓶數。
三、打完第 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


3.Lotto

為了呼應台灣電腦彩券的發行,我們再次推出跟組合有關的題目。你在買彩券的時候一定會挑你喜歡的數字吧!(雖然理論上不會增加你的中獎機率,但是你還是會選擇你的Lucky Number)我們的問題是:假設共有49個號碼,而你必須在你的 k (k>6)個Lucky Number中挑6個號碼作為一張彩券的數字組合。例如:你的Lucky Number的集合是{1,2,3,5,8,13,21,34}以就是說 k=8 ,那麼你就有C(8,6)=28種可能的彩券組合:
[1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], ..., [3,5,8,13,21,34].

你的任務是讀入k以及Lucky Number的集合,然後輸出所有可能的組合。

輸入說明 :
每筆測試資料一行,每行的第1個整數代表 k( 6 < k < 1 3 ) 。 接下來的k個整數代表Lucky Number的集合,此集合已經按數字由小到大排好。k=0代表輸入結束。

輸出說明 :
對每一筆測試資料,輸出其所有可能的組合,每個組合一行。請注意輸出組合的順序需由小到大排列。測試資料之間請空一行。請參考Sample Output。

範例輸入 :

7 1 2 3 4 5 6 7
8 1 2 3 5 8 13 21 34
0
範例輸出 :

1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 6 7
1 2 3 5 6 7
1 2 4 5 6 7
1 3 4 5 6 7
2 3 4 5 6 7

1 2 3 5 8 13
1 2 3 5 8 21
1 2 3 5 8 34
1 2 3 5 13 21
1 2 3 5 13 34
1 2 3 5 21 34
1 2 3 8 13 21
1 2 3 8 13 34
1 2 3 8 21 34
1 2 3 13 21 34
1 2 5 8 13 21
1 2 5 8 13 34
1 2 5 8 21 34
1 2 5 13 21 34
1 2 8 13 21 34
1 3 5 8 13 21
1 3 5 8 13 34
1 3 5 8 21 34
1 3 5 13 21 34
1 3 8 13 21 34
1 5 8 13 21 34
2 3 5 8 13 21
2 3 5 8 13 34
2 3 5 8 21 34
2 3 5 13 21 34
2 3 8 13 21 34
2 5 8 13 21 34
3 5 8 13 21 34


4 則留言:

  1. 1.

    Private Sub Form_Load()
    Me.Hide
    Dim K(9) As Integer
    Dim x(10, 10) As Integer
    Open App.Path & "\test3.txt" For Input As #1
    Open App.Path & "\result3.txt" For Output As #2
    n = 0
    For i = 1 To 9
    Line Input #1, X1
    a = Split(X1, " ")
    n = n + 1
    For i1 = 0 To 8
    x(n, i1 + 1) = a(i1)
    Next
    Next
    Do While Not EOF(1)

    For i = 1 To 9
    K(i) = 0
    Next
    Input #1, X2, Y2
    If X2 = 0 And Y2 = 0 Then Exit Do
    Select Case Y2
    Case 1 To 3
    x3 = 2
    Case 4 To 6
    x3 = 5
    Case 7 To 9
    x3 = 8
    End Select
    Select Case X2
    Case 1 To 3
    y3 = 2
    Case 4 To 6
    y3 = 5
    Case 7 To 9
    y3 = 8
    End Select
    For i = 1 To 9
    If x(i, X2) <> 0 Then K(x(i, X2)) = 1
    If x(Y2, i) <> 0 Then K(x(Y2, i)) = 1
    Next
    If x(x3 + 1, y3 + 1) <> 0 Then K(x(x3 + 1, y3 + 1)) = 1
    If x(x3 + 1, y3 - 1) <> 0 Then K(x(x3 + 1, y3 - 1)) = 1
    If x(x3 + 1, y3) <> 0 Then K(x(x3 + 1, y3)) = 1
    If x(x3 - 1, y3 + 1) <> 0 Then K(x(x3 - 1, y3 + 1)) = 1
    If x(x3 - 1, y3 - 1) <> 0 Then K(x(x3 - 1, y3 - 1)) = 1
    If x(x3 - 1, y3) <> 0 Then K(x(x3 - 1, y3)) = 1
    If x(x3, y3 + 1) <> 0 Then K(x(x3, y3 + 1)) = 1
    If x(x3, y3 - 1) <> 0 Then K(x(x3, y3 - 1)) = 1
    If x(x3, y3) <> 0 Then K(x(x3, y3)) = 1
    ch = False
    If x(Y2, X2) = 0 Then
    For i = 1 To 9
    If K(i) = 0 Then
    Print #2, i,
    ch = True
    End If
    Next
    End If
    If ch = False Then Print #2, 0
    Print #2,

    Loop
    Close
    Close
    End
    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
    Input #1, n
    For i = 1 To n
    Line Input #1, x
    a = Split(x, " ")
    b = Split(x, " ")
    For j = 1 To Len(x)
    If Mid(x, j, 1) <> " " Then
    If Mid(x, j, 1) = "-" Then
    List1.AddItem "0"
    ElseIf Mid(x, j, 1) = "/" Then List1.AddItem "10"
    ElseIf Mid(x, j, 1) = "X" Then List1.AddItem "10"
    Else
    List1.AddItem Mid(x, j, 1)
    End If
    List2.AddItem Mid(x, j, 1)
    End If
    Next
    List1.AddItem 0
    List1.AddItem 0
    ans = 0
    For j = 0 To List2.ListCount - 2
    Select Case List2.List(j)
    Case "-"
    Case "/"
    ans = ans + 10 + Val(List1.List(j + 1))
    ans = ans - Val(List1.List(j - 1))
    Case "X"
    ans = ans + 10
    If List2.List(j + 2) = "/" Then
    ans = ans + 10
    Else
    If j <> (List2.ListCount - 3) Then
    ans = ans + Val(List1.List(j + 1)) + Val(List1.List(j + 2))
    End If
    End If
    Case Else
    ans = ans + Val(List1.List(j))
    End Select
    Next
    List1.Clear
    List2.Clear
    Print #2, ans
    Next
    Close
    Close
    End
    End Sub

    3.
    Dim a
    Dim ch As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)

    Line Input #1, x
    If x = 0 Then End
    a = Split(x, " ")
    Call abc("", 0)



    For i = 0 To List1.ListCount - 1
    Print #2, List1.List(i)
    Next
    Print #2,
    List1.Clear
    Loop
    Close
    Close
    End
    End Sub
    Function abc(x, m)
    Dim num(6) As Integer
    If m Mod 6 = 0 And m <> 0 Then
    ch = True
    For i = 0 To List1.ListCount - 1
    n = 0
    f = Split(List1.List(i), " ")
    g = Split(x, " ")

    For k = 1 To 6
    For k1 = 1 To 6
    If f(k) = g(k1) Then n = n + 1
    Next
    Next

    If List1.List(i) = x Or n >= 6 Then ch = False
    Next
    If ch = True Then List1.AddItem x
    Else
    For i = 1 To UBound(a)
    If InStr(x, a(i)) = 0 Then Call abc(x & " " & a(i), m + 1)
    Next
    End If
    End Function

    回覆刪除
  2. -1-------------------------------------------------------
    Dim sudo(9) As String
    Dim num(81)
    Dim b(9, 9)
    Dim t As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\test3.txt" For Input As #1
    Open App.Path & "\result3.txt" For Output As #2
    For i = 1 To 9
    Line Input #1, sudo(i)
    Next i
    c = 1
    For i = 1 To 9
    a = Split(sudo(i), " ")
    For m = 0 To 8
    num(c) = a(m)
    c = c + 1
    Next m
    Next i

    'For i = 1 To 81
    'Print #2, num(i);
    'If i Mod 9 = 0 Then Print #2,
    'Next i

    c = 1
    For i = 1 To 9
    For j = 1 To 9
    b(i, j) = num(c)
    c = c + 1
    'Print #2, b(i, j);
    Next j
    'Print #2,
    Next i

    Do
    Input #1, y, x
    If y = 0 And x = 0 Then Exit Do
    If b(x, y) = 0 Then
    Call check(x, y)
    For j = 1 To 9
    For i = 0 To List1.ListCount - 1
    If j = List1.List(i) Then t = False: Exit For Else t = True
    Next i
    If t = True Then Print #2, j;
    Next j
    Print #2,
    Else
    Print #2, 0
    End If
    List1.Clear
    Loop


    Close #2
    Close #1
    End
    End Sub

    Sub check(ByVal x, ByVal y)
    For i = 1 To 9
    If i <> x Then
    If b(i, y) <> 0 Then List1.AddItem Val(b(i, y))
    End If
    Next i

    For i = 1 To 9
    If i <> y Then
    If b(x, i) <> 0 Then List1.AddItem Val(b(x, i))
    End If
    Next i

    Select Case x
    Case 1, 2, 3
    Select Case y
    Case 1, 2, 3
    For i = 1 To 3
    For j = 1 To 3
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 4, 5, 6
    For i = 1 To 3
    For j = 4 To 6
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 7, 8, 9
    For i = 1 To 3
    For j = 7 To 9
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    End Select
    Case 4, 5, 6
    Select Case y
    Case 1, 2, 3
    For i = 4 To 6
    For j = 1 To 3
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 4, 5, 6
    For i = 4 To 6
    For j = 4 To 6
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i

    Case 7, 8, 9
    For i = 4 To 6
    For j = 7 To 9
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    End Select
    Case 7, 8, 9
    Select Case y
    Case 1, 2, 3
    For i = 7 To 9
    For j = 1 To 3
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    Case 4, 5, 6
    For i = 7 To 9
    For j = 4 To 6
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    Case 7, 8, 9
    For i = 7 To 9
    For j = 7 To 9
    If i <> x And j <> y Then
    If b(i, j) <> 0 Then List1.AddItem b(i, j)
    End If
    Next j
    Next i
    End Select
    End Select
    End Sub
    in:
    4 0 0 8 0 5 0 1 0
    2 0 0 0 0 9 0 8 0
    6 0 8 0 0 3 4 9 0
    0 0 5 0 2 0 6 0 0
    0 0 1 0 7 0 9 0 0
    0 0 7 0 8 0 3 0 0
    0 9 0 1 0 0 0 0 2
    0 8 0 4 0 0 0 0 9
    0 3 0 6 0 2 0 0 7
    5 3
    6 7
    5 5
    0 0

    out:
    1
    7 8
    0

    回覆刪除

  3. -2------------------------------------------------------
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\test3.txt" For Input As #1
    Open App.Path & "\result3.txt" For Output As #2
    Input #1, n
    For iii = 1 To n
    Sum = 0

    Line Input #1, s
    's = Replace(s, "-", 0)
    s = Replace(s, " ", "")
    For i = 1 To Len(s)
    x = Mid(s, i, 1)
    Select Case x
    Case 1, 2, 3, 4, 5, 6, 7, 8, 9
    If Mid(s, i + 1, 1) = "-" Then Sum = Sum + Val(x)
    'Sum = Sum + x
    If Mid(s, i + 1, 1) = "/" Then
    Sum = Sum + 10
    If Mid(s, i + 2, 1) = "X" Then
    Sum = Sum + 10
    Else
    Sum = Sum + Val(Mid(s, i + 2, 1))
    End If
    End If

    If Mid(s, i + 1, 1) = "1" Or Mid(s, i + 1, 1) = "2" Or Mid(s, i + 1, 1) = "3" Or Mid(s, i + 1, 1) = "4" Or Mid(s, i + 1, 1) = "5" Or Mid(s, i + 1, 1) = "6" Or Mid(s, i + 1, 1) = "7" Or Mid(s, i + 1, 1) = "8" Or Mid(s, i + 1, 1) = "9" Then
    Sum = Sum + Val(Mid(s, i + 1, 1))
    End If
    Case "X"
    Sum = Sum + 10
    If i > 1 Then
    'If Mid(s, i - 1, 1) = "/" Then Sum = Sum + 10
    'If Mid(s, i - 1, 1) = "-" Then Sum = sum + 10- Val(Mid(s, i - 2, 1))
    If Mid(s, i + 1, 1) = "X" Then Sum = Sum + 10
    If Mid(s, i + 2, 1) = "X" Then Sum = Sum + 10
    If Mid(s, i + 1, 1) = "1" Or Mid(s, i + 1, 1) = "2" Or Mid(s, i + 1, 1) = "3" Or Mid(s, i + 1, 1) = "4" Or Mid(s, i + 1, 1) = "5" Or Mid(s, i + 1, 1) = "6" Or Mid(s, i + 1, 1) = "7" Or Mid(s, i + 1, 1) = "8" Or Mid(s, i + 1, 1) = "9" Then Sum = Sum + Val(Mid(s, i + 1, 1))
    If Mid(s, i + 2, 1) = "1" Or Mid(s, i + 2, 1) = "2" Or Mid(s, i + 2, 1) = "3" Or Mid(s, i + 2, 1) = "4" Or Mid(s, i + 2, 1) = "5" Or Mid(s, i + 2, 1) = "6" Or Mid(s, i + 2, 1) = "7" Or Mid(s, i + 2, 1) = "8" Or Mid(s, i + 2, 1) = "9" Then Sum = Sum + Val(Mid(s, i + 2, 1))
    If Mid(s, i + 2, 1) = "/" Then Sum = Sum + 10 - Val(Mid(s, i + 1, 1))
    End If
    End Select
    Next i
    If Right(s, 2) = "XX" Then Sum = Sum - 10
    Print #2, Sum
    Next iii
    Close #2
    Close #1
    End
    End Sub

    in:
    2
    7- 8/ X 8- X X X X X 8/9
    X X X X X X X X X XXX

    out:
    210
    300

    回覆刪除
  4. Dim a()
    Dim r As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\test3.txt" For Input As #1
    Open App.Path & "\result3.txt" For Output As #2
    Do
    Input #1, n
    If n = 0 Then Exit Do
    ReDim a(n)
    For i = 1 To n
    Input #1, a(i)
    List1.AddItem a(i)
    Next i
    Call make("", 0, 0)
    List1.Clear
    Print #2,
    Loop
    Close #2
    Close #1
    End
    End Sub

    Sub make(ByVal x, ByVal y, ByVal z)
    If z = 6 Then
    Print #2, x
    Else
    For i = y To UBound(a) - 1
    If z < 6 Then Call make(x & " " & List1.List(i), i + 1, z + 1)
    Next i
    End If
    End Sub

    回覆刪除