2012年11月20日 星期二

數字刪除

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

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

輸出範例:
7
5

3 則留言:

  1. Dim mycount, ans As Integer
    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, mycount
    For ai = 1 To mycount
    Line Input #1, strr
    strr = Split(strr, " ")
    times = UBound(strr)
    For i = 0 To times
    For ii = 0 To times
    If strr(i) = strr(ii) And i <> ii Then strr(ii) = "": strr(i) = ""
    Next
    Next

    For iii = 0 To times
    If strr(iii) <> "" Then ans = ans + 1
    Next
    Print #2, ans
    ans = 0
    Next

    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim b As Boolean
    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
    Line Input #1, x
    z = 0
    m = Split(x, " ")
    For j = 0 To UBound(m)
    b = True
    For k = 0 To UBound(m)
    If m(j) = m(k) And j <> k Then b = False
    Next
    If b = True Then z = z + 1
    Next
    Print #2, z
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. 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, z
    For q = 1 To z
    Line Input #1, n
    x = Split(n)
    For i = 0 To UBound(x)
    List1.AddItem x(i)
    k = List1.ListCount
    Next
    For i = 0 To UBound(x)
    For j = 0 To UBound(x)
    If List1.List(i) = List1.List(j) And i <> j Then k = k - 1
    Next
    Next
    Print #2, k
    List1.Clear
    k = 0
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除