2011年5月2日 星期一

今晚打老虎

這台機器有三顆功能鍵跟數字小鍵盤


功能鈕上分別寫著


1. Insert


2. Query MAX


3. Query MIN


旁邊寫著一行粗字: 極值經查詢後將會刪除 


題目看到這各位也明瞭了吧


請你寫出這台機器的程式


可以插入數字並且查詢其中的最大值與最小值 


輸入說明 :
每行輸入開頭有三種情形


1: 插入操作,其後會跟著一數字 N 代表插入的數字 (0 ≤ N ≤ 2^31-1)
2: 查詢最大值
3: 查詢最小值
同一時間內最多有 100,0000 個數字


輸出說明 :
每筆查詢輸出一行


每行只有一個數字 




範例輸入 :


1 3
1 100 

1 4 
3


範例輸出 :


100 
3


轉載自http://cat.nknush.kh.edu.tw/ShowProblem?problemid=a091

6 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim s
    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, a

    s = Split(a)

    Select Case s(0)
    Case 1
    List1.AddItem s(1)
    Case 2
    Call myMax
    Case 3
    Call myMin
    End Select

    Loop

    Close
    Close
    End
    End Sub

    Sub myMax()
    Dim max As Long
    max = 0

    For i = 0 To List1.ListCount - 1
    If Val(List1.List(i)) > max Then max = List1.List(i)
    Next

    Print #2, max

    List2.Clear

    For i = 0 To List1.ListCount - 1
    If Val(List1.List(i)) <> max Then List2.AddItem List1.List(i) Else max = -1
    Next

    List1.Clear

    For i = 0 To List2.ListCount - 1
    List1.AddItem List2.List(i)
    Next


    End Sub

    Sub myMin()
    Dim min As Long

    min = List1.List(0)

    For j = 0 To List1.ListCount - 1
    For i = 0 To List1.ListCount - 1
    If List1.List(i) < min Then min = List1.List(i)
    Next
    Next


    Print #2, min

    List2.Clear

    For i = 0 To List1.ListCount - 1
    If Val(List1.List(i)) <> min Then List2.AddItem List1.List(i) Else min = -1
    Next

    List1.Clear

    For i = 0 To List2.ListCount - 1
    List1.AddItem List2.List(i)
    Next


    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Dim Insert As Integer
    Dim Max As Integer
    Dim Min As Integer

    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, T
    Select Case T
    Case 1
    Input #1, Insert
    List1.AddItem Insert

    Case 2
    Max = List1.List(0)
    For i = 1 To List1.ListCount
    If Max < List1.List(i - 1) Then Max = List1.List(i - 1)
    Next i
    Print #2, Max

    Case 3
    Min = List1.List(0)
    For i = 1 To List1.ListCount
    If Min > List1.List(i - 1) Then Max = List1.List(i - 1)
    Next i
    Print #2, Min

    End Select

    Loop
    Close #2
    Close #1

    End
    End Sub

    回覆刪除
  3. 佑好,
    你的程式少了將查詢過的數,給刪掉的動作了。

    arro好,
    程式ok。
    找最大(小)與重新加入另一個listbox可以放在一起。
    ( 以第1個數當極值,開始往後比,每次比較,不是極值的那個數,加入list2就好了。)

    另外,既然是在比最大最小,之前不是試過將它變成字串後,放進listbox中嗎?
    然後使用list本身的排序功能嗎?

    回覆刪除
  4. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Do Until EOF(1)
    Input #1, n
    Select Case n
    Case 1
    Input #1, a
    List1.AddItem a
    Case 2
    List2.Clear
    b = Val(List1.List(0))
    For i = 1 To List1.ListCount - 1
    If b < Val(List1.List(i)) Then List2.AddItem b: b = Val(List1.List(i)) Else List2.AddItem List1.List(i)
    Next
    Print #2, b
    List1.Clear
    For i = 0 To List2.ListCount - 1
    List1.AddItem List2.List(i)
    Next
    Case 3
    List2.Clear
    s = Val(List1.List(0))
    For i = 1 To List1.ListCount - 1
    If s > Val(List1.List(i)) Then List2.AddItem s: s = Val(List1.List(i)) Else List2.AddItem List1.List(i)
    Next
    Print #2, s
    List1.Clear
    For i = 0 To List2.ListCount - 1
    List1.AddItem List2.List(i)
    Next
    End Select
    Loop
    Close #1
    Close #2
    End
    End Sub
    ------------------
    in.txt
    1 3
    1 100
    2
    1 5
    2
    1 4
    1 1
    3
    ------------------
    out.txt
    100
    5
    1

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Dim Insert As Integer
    Dim Max As Integer
    Dim Min As Integer

    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, T
    Select Case T
    Case 1
    Input #1, Insert
    List1.AddItem Insert

    Case 2
    Max = List1.List(0)
    For i = 1 To List1.ListCount
    If Max < List1.List(i - 1) Then Max = List1.List(i - 1): List1.RemoveItem (i - 1)
    Next i
    Print #2, Max

    Case 3
    Min = List1.List(0)
    For i = 1 To List1.ListCount
    If Min > List1.List(i - 1) Then Min = List1.List(i - 1): List1.RemoveItem (i - 1)
    Next i
    Print #2, Min

    End Select

    Loop
    Close #2
    Close #1

    End
    End Sub

    ------------------
    in.txt
    1 3
    1 100
    2
    1 5
    2
    1 4
    1 1
    3
    ------------------
    out.txt
    100
    5
    1

    回覆刪除
  6. 緣尉好,
    程式正確也清楚,很好。
    佑好,
    你的程式中的
    list1.removeitem (i-1)
    會不會在不同的情況裡,執行超過一次,而發生錯誤的結果呢。

    回覆刪除