2011年9月5日 星期一

九月小考




100程式設計選手隊小考
題1:排列組合
有x顆紅色小玻璃珠和y顆黑色小玻璃珠在一個黑箱子,一把取出z個,請寫程式列出共有幾種組合。
輸入說明:in1.txt只有1行數字,分別表示x,y,z。
5,3,3
輸出說明:以紅黑來表示玻璃珠,輸出到out1.txt
紅紅紅
紅紅黑
紅黑黑
黑黑黑

題2:數字方陣
請將數字1,2,3,…,n^2,排成n*n的正方形矩陣,使用每個橫列直列和兩個對角線加起來的和,都相等。(n可以是3,5,7,9)
輸入:in2.txt輸入n值
5
輸出:out2.txt,為使數字對齊,個位數請加個0
17 24 01 08 15
23 05 07 14 16
04 06 13 20 22
10 12 19 21 03
11 18 25 02 09


題3:倍數問題
有一個50位的大數,請寫程式計算它是不是197的倍數。
輸入:in3.txt每行是個50位數的大數,0表示結束。
19719719719719719719719719719719719719719719719700
38400000000000000000000000000000000000000000000001
0
輸出:out3.txt

不是

6 則留言:

  1. 網誌管理員已經移除這則留言。

    回覆刪除
  2. 1.
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Input #1, X, Y, z
    For i = 1 To X
    ans = ans & 0
    Next i

    For i = 1 To Y
    ans = ans & 1
    Next i

    Call ABC("", z, 1)

    For i = 0 To List1.ListCount - 1
    ans2 = Replace(List1.List(i), 0, "紅")
    ans2 = Replace(ans2, 1, "黑")
    Print #2, ans2
    Next i

    Close #2
    Close #1
    End
    End Sub

    Sub ABC(A, B, C)
    If Len(A) = z Then
    If RE(A) = False Then List1.AddItem A
    Else
    For i = 1 To Len(ans)
    If i > C Then Call ABC(A & Mid(ans, i, 1), "", i)
    Next i
    End If
    End Sub

    Function RE(A) As Boolean
    p = False
    For i = 0 To List1.ListCount - 1
    If List1.List(i) = A Then p = True: Exit For
    Next i
    RE = p
    End Function

    -----------------------------------------------------------------
    2.
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Input #1, N
    ReDim A(N, N) As String
    X = 1
    Y = (1 + N) / 2
    A(X, Y) = "01"
    For i = 2 To N ^ 2
    RX = X + 2: If RX > N Then RX = RX - N
    RY = Y + 1: If RY > N Then RY = RY - N
    If A(RX, RY) = "" Then
    X = RX
    Y = RY
    If i < 10 Then A(X, Y) = "0" & i Else A(X, Y) = i
    Else
    X = X + 1: If X > N Then X = X - N
    If i < 10 Then A(X, Y) = "0" & i Else A(X, Y) = i
    End If
    Next i

    For i = 1 To N
    ans = ""
    For j = 1 To N
    ans = ans & A(i, j) & " "
    Next j
    Print #2, ans
    Next i

    Close #2
    Close #1
    End
    End Sub
    ----------------------------------------------------
    3.
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out3.txt" For Output As #2
    Do While Not EOF(1)
    Line Input #1, N
    If Len(N) <> 50 Then Exit Do

    Do Until Len(N) < 3
    A = Val(Mid(M, 1, 3))
    B = A Mod 197
    N = Right(N, Len(N) - 3)
    If B <> 0 Then N = B & N
    Loop

    If Val(N) = 0 Then Print #2, "是" Else Print #2, "不是"

    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Dim x, y, z, lines, s
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Input #1, x, y, z
    For i = 1 To x
    lines = lines & " 紅"
    Next
    For i = 1 To y
    lines = lines & " 黑"
    Next
    s = Split(lines)
    Call Run("", "", 0)


    Close
    Close
    End
    End Sub

    Sub Run(ans, tmp, times)
    If times = z Then
    Call Final2(ans)
    Else
    For i = 1 To x + y
    If InStr(tmp, i) = 0 Then Call Run(ans & " " & s(i), tmp & " " & i, times + 1)
    Next
    End If
    End Sub


    Sub Final2(a)
    Dim m, red, black
    m = Split(a)
    For i = 0 To UBound(m)
    If m(i) = "紅" Then red = red + 1
    If m(i) = "黑" Then black = black + 1
    Next

    Dim ch As Boolean
    ch = False
    For i = 0 To List1.ListCount - 1
    If red & "*" & black = List1.List(i) Then ch = True
    Next
    If ch = False Then List1.AddItem (red & "*" & black): Print #2, a

    End Sub


    '---------------------------------------------------------------------------


    Dim s, final, x, y, n
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Input #1, n
    ReDim s(n, n)
    final = n ^ 2
    x = 1: y = (n + 1) / 2

    For i = 1 To final
    If i < 10 Then i = " 0" & i
    s(x, y) = i
    If ch(x, y) = True Then Call mix(x, y) Else Call mix2(x, y)
    Next


    For i = 1 To n
    For j = 1 To n
    Print #2, s(i, j),
    Next
    Print #2, ""
    Next
    Close
    Close

    End
    End Sub

    Function ch(ByVal a, ByVal b)
    ch = True
    Call mix(a, b)
    If s(a, b) <> 0 Then ch = False
    End Function

    Function mix(a, b)
    If a - 1 <= 0 Then a = n Else a = a - 1
    If b + 1 > n Then b = 1 Else b = b + 1
    End Function

    Function mix2(a, b)
    If a + 1 > n Then a = 1 Else a = a + 1
    End Function



    '---------------------------------------------------------------------------


    Dim a As String, x As String, y As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out3.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, a
    If a = 0 Then Exit Do
    '197
    y = 0: x = 0
    For i = 1 To Len(a) Step 3
    x = Val(Mid(a, i, 3)) + (Val(y) * (10 ^ 3))
    y = x Mod 197
    x = 0
    Next
    If y = 0 Then Print #2, "是" Else Print #2, "不是"
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  4. 佑好,
    第1題錯的,去試了試執行,沒有結果出來。
    再來,你也沒去管拿出來的玻璃珠會不會比該有的單色多。
    (3個黑珠,拿4出4個珠子都是黑色的,是錯的。)

    第2題正確。用的是另外的方法,滿好的。
    第3題錯的,去試了執行,出來錯的答案,找了好久,
    A = Val(Mid(M, 1, 3))
    這行程式中的,m,不存在吧。
    arro好,
    第1題正確。
    第2題正確。(這是我熟悉的方式,OK)
    第3題正確。

    回覆刪除
  5. 1.漏掉複製最上面的兩個定義
    Dim ans As String
    Dim z


    2.
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out3.txt" For Output As #2
    Do While Not EOF(1)
    Line Input #1, N
    If Len(N) <> 50 Then Exit Do

    Do Until Len(N) < 4
    A = Val(Mid(N, 1, 4))
    B = A Mod 197
    N = Right(N, Len(N) - 4)
    If B <> 0 Then N = B & N
    Loop

    If Val(N) = 0 Then Print #2, "是" Else Print #2, "不是"

    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  6. 熊掌好,

    1.在
    For i = 1 To X
    ans = ans & 0
    Next i

    For i = 1 To Y
    ans = ans & 1
    Next i

    有去管黑紅珠有幾個:D
    in.txt-------------
    2,3,3
    out.txt------------
    紅紅黑
    紅黑黑
    黑黑黑


    Dim ans As String
    Dim z
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Input #1, X, Y, z
    For i = 1 To X
    ans = ans & "紅"
    Next i

    For i = 1 To Y
    ans = ans & "黑"
    Next i

    Call ABC("", z, 0)'位子從零開始才對

    For i = 0 To List1.ListCount - 1
    Print #2, List1.List(i)
    Next i

    Close #2
    Close #1
    End
    End Sub

    Sub ABC(A, B, C)
    If Len(A) = z Then
    If RE(A) = False Then List1.AddItem A
    Else
    For i = 1 To Len(ans)
    If i > C Then Call ABC(A & Mid(ans, i, 1), "", i)
    Next i
    End If
    End Sub

    Function RE(A) As Boolean
    p = False
    For i = 0 To List1.ListCount - 1
    If List1.List(i) = A Then p = True: Exit For
    Next i
    RE = p
    End Function

    回覆刪除