2011年10月21日 星期五

97正式 Problem3:黑盒子

Problem3黑盒子(12%)

有一個黑盒子支援 Insert, Inquire, Clean, End四種指令:

l Insert x:將x放入黑盒子中。

l Inquire k:輸出黑盒子當中第k大的數字,若找不到該數,則不輸出。

l Clean:更換另一組測試資料,清空黑盒子。

l End:結束程式。

輸入說明:

請參考範例輸入,所有數字都為整數。一組測試資料的指令數不會超過1000個。

輸出說明:

請將每一個合法的Inquire k值輸出。

輸入範例:

Insert 4

Inquire 1

Insert 1

Inquire 2

Insert -3

Insert 3

Insert 7

Insert -99

Inquire 4

Clean

Insert 3

Inquire 2

Inquire 1

END

輸出範例:

4

1

1

3

2 則留言:

  1. 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, Q
    X = Split(Q)

    Select Case X(0)
    Case Is = "Insert": List1.AddItem X(1)
    Case Is = "Inquire": Call ABC(Val(X(1)))
    Case Is = "Clean": List1.Clear
    Case Is = "END": End
    End Select

    Loop
    Close #2
    Close #1
    End
    End Sub


    Sub ABC(A)
    ReDim Y(List1.ListCount - 1)
    For i = 0 To List1.ListCount - 1
    Y(i) = Val(List1.List(i))
    Next i

    For i = 0 To UBound(Y) - 1
    For j = i + 1 To UBound(Y)
    If Y(i) < Y(j) Then
    T = Y(i)
    Y(i) = Y(j)
    Y(j) = T
    End If
    Next j
    Next i
    If UBound(Y) >= A - 1 Then Print #2, Y(A - 1)
    End Sub

    回覆刪除
  2. Dim cmd, BB, s
    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, cmd
    Select Case cmd
    Case "Clean": List1.Clear
    Case "End": Exit Do
    Case Else
    s = Split(cmd)
    Select Case s(0)
    Case "Insert": Call AA(s(1))
    Case "Inquire": Call A1(s(1))
    End Select
    End Select
    Loop
    Close
    Close
    End
    End Sub
    Sub AA(a)

    If InStr(a, "-") <> 0 Then a = Replace(a, "-", "&")
    Do While Len(a) <> 10: a = "0" & a: Loop
    List1.AddItem a

    End Sub
    Sub A1(a)

    If a > List1.ListCount Then Exit Sub

    k = List1.List(List1.ListCount - a)
    Do While Left(k, 1) = 0: k = Right(k, Len(k) - 1): Loop
    If InStr(k, "&") <> 0 Then k = Replace(k, "&", "-")
    Print #2, k
    End Sub

    回覆刪除