2012年6月22日 星期五

數字刪除

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

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

輸出範例:
7
5

4 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim f(10000)
    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
    a = Split(x)
    For j = 0 To UBound(a)
    f(a(j)) = f(a(j)) + 1
    Next
    ans = 0
    For k = 0 To 10000
    If f(k) = 1 Then ans = ans + 1
    Next
    Print #2, ans
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  2. 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(1000) As Integer
    Input #1, n
    For i = 1 To n
    Line Input #1, a
    x = Split(a)
    For j = 0 To UBound(x)
    num(Val(x(j))) = num(Val(x(j))) + 1
    Next j
    ans = 0
    For k = 1 To 1000
    If num(k) = 1 Then ans = ans + 1
    Next k
    Print #2, ans
    Next i
    Close
    Close
    End
    End Sub

    回覆刪除
  3. 小冰、晟晟好,
    程式都正確。
    這題的解題技巧也很好。

    回覆刪除
  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, n
    For i = 1 To Val(n)
    Line Input #1, a
    b = Split(a)
    ans = 0
    For j = 0 To UBound(b)
    For r = j To UBound(b)
    If j <> r Then
    If InStr(b(r), b(j)) > 0 Then b(r) = "": b(j) = ""
    End If
    Next r
    Next j
    For j = 0 To UBound(b)
    If b(j) <> "" Then ans = ans + 1
    Next j
    Print #2, ans
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除