2012年11月5日 星期一

第一次儲備選手選拔試卷第一題


找出最小數字:從指定目錄中的"in.txt",讀入一數字字串,以逗號分隔,找出字串中最小字數。

輸出到指定目錄的"out.txt"。
(讀取指定目錄的in.txt, 請用 open app.path & "\in.txt" for input as #1 )
※今後皆以指定目錄形式完成VB

輸入範例:
26,56,78,99,15,13

輸出範例:
13

5 則留言:

  1. Dim str As String
    Dim k As Long
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Line Input #1, str
    Close #1

    num = Split(str, ",")

    k = 99999
    For i = 0 To UBound(num)
    If num(i) < k Then k = num(i)
    Next

    Open App.Path & "\out.txt" For Output As #1
    Print #1, k
    Close #1
    End Sub

    回覆刪除
  2. Bob好
    程式錯誤,用long很好,但如果輸入範例比K大呢?
    想想看吧:)

    回覆刪除
  3. Dim str As String
    Dim k As Long
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Line Input #1, str
    Close #1

    num = Split(str, ",")

    k = num(0)
    For i = 1 To UBound(num)
    If num(i) < k Then k = num(i)
    Next

    Open App.Path & "\out.txt" For Output As #1
    Print #1, k
    Close #1
    End Sub

    回覆刪除
  4. 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, Min
    Do Until EOF(1)
    Input #1, x
    If Min > x Then Min = x
    Loop
    Print #2, Min
    Close
    Close
    End
    End Sub

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim num(5) As Integer
    For j = 0 To 5
    Input #1, n
    num(j) = n
    Next
    For k = 0 To UBound(num)
    For h = 0 To UBound(num) - 1
    If Val(num(h)) > Val(num(h + 1)) Then
    x = num(h)
    num(h) = num(h + 1)
    num(h + 1) = x
    End If
    Next
    Next
    Print #2, num(0)
    Close
    Close
    End
    End Sub

    回覆刪除