2011年10月5日 星期三

數字刪除

給n 個數字,請你在這n 個數字中,找出所有重覆出現的數字,並把它刪除。最後計算
刪除後剩餘的數字個數。
輸入說明:
第一行的數字,表示有幾組測試資料,第二行開始即為第一筆測試資料,每行不會超過
100 個數字,每個數字之間用一個空白做為區隔,數字的範圍為[0, 10000]間的整數。
輸出說明:
對輸入的每筆測試資料,分別輸出刪除完後剩下的數字個數。

輸入範例:
2
1 2 3 4 5 6 7 8 9 2 1
2 4 6 8 10

輸出範例:
7
5

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
    Input #1, n
    For i = 1 To n
    List1.Clear: List2.Clear
    Line Input #1, X
    Y = Split(X)
    For j = 0 To UBound(Y)
    List1.AddItem Y(j)
    Next j


    For j = 0 To (List1.ListCount - 2)
    For r = j + 1 To (List1.ListCount - 1)
    If List1.List(j) = List1.List(r) Then
    Call ABC(List1.List(j))
    End If
    Next r
    Next j

    For j = 0 To List1.ListCount - 1
    If Left(List1.List(j), 1) <> "X" Then List2.AddItem List1.List(j)
    Next j

    Print #2, List2.ListCount

    Next i
    Close #2
    Close #1
    End
    End Sub

    Sub ABC(A)
    For i = 0 To List1.ListCount - 1
    If A = List1.List(i) Then List1.List(i) = "X" & List1.List(i)
    Next i
    End Sub

    in.txt---------------
    3
    1 2 3 4 5 6 7 8 9 2 1
    2 4 6 8 10
    1000 123456 1 2 3 32 2 1 100 1000 9999 9 2

    out.txt---------------
    7
    5
    6

    回覆刪除
  2. Dim m, bar As String
    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, T

    For i = 1 To T
    Line Input #1, n
    m = Split(n)
    bar = ""
    Dim A: A = 0

    For k = 0 To UBound(m)
    If InStr(bar, m(k)) <> 0 Then A = A - 1: MsgBox m(k) Else A = A + 1
    bar = bar & " " & m(k)
    Next

    Print #2, A
    Next

    Close
    Close
    End
    End Sub

    回覆刪除