2012年7月12日 星期四

竊車問題 (Lostcar)

一位警察發現了一些可疑的機車,為了查明這些可疑的機車是否為失竊機車,他需將這些機車的車號與警政署的失竊機車檔案比對。請寫一程式來幫助這位警察找出哪些是失竊的機車。


條件限制
警察發現的可疑機車不超過20輛。 
警政署的失竊機車檔案中最多為100輛機車資料。 


輸入檔格式
第一行有兩個整數n和m,中間以一個空白分開。n為發現的可疑機車數目,m為失竊機車檔案中機車的數目。接下來的n行,每行有一可疑機車的車號,車號以六個字元表示。再接下來的m行,則為失竊機車資料,每行有三項資料,各項資料間以一個空白隔開;第一項資料(欄位1~6)為機車車號,第二項(欄位8~13)為該車外觀顏色,第三項(欄位15~20)為該車車主姓名。


輸出檔格式 
請依車牌的順序(英文字母由A~Z)依序印出找到的失竊機車資料。


輸入範例
4 6
PIG222
WIN555
SAD321
JOY866
DOG999 RED CHANG 
JOY355 BLUE LEE 
SAD321 YELLOW WANG 
FOX555 WHITE WU 
WIN555 BROWN HO 
PIG222 BLACK LIN 


輸出範例


PIG222 BLACK LIN 
SAD321 YELLOW WANG
WIN555 BROWN HO 

3 則留言:

  1. Private Sub Form_Load()
    Dim n(20), m(100) As String
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n1, m1
    For i = 1 To n1
    Line Input #1, x
    n(i) = x
    Next
    For i = 1 To m1
    Line Input #1, x
    m(i) = x
    Next
    For i = 1 To n1
    For j = 1 To m1
    If n(i) = Left(m(j), 6) Then List1.AddItem m(j): Exit For
    Next
    Next
    For i = 0 To List1.ListCount
    Print #2, List1.List(i)
    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 n(20), m(100) As String
    Input #1, n1, m1
    For i1 = 1 To n1
    Line Input #1, n(i1)
    Next i1
    For i2 = 1 To m1
    Line Input #1, m(i2)
    Next i2
    For i = 1 To n1
    For j = 1 To m1
    If n(i) = Left(m(j), 6) Then Print #2, n(i) & Replace(m(j), n(i), "")
    Next j
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Dim a(), b() As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n, m
    ReDim a(Val(n)), b(Val(m), 3)
    For i = 1 To Val(n)
    Input #1, x
    a(i) = x
    Next i
    For i = 2 To Val(n)
    If Left(a(i), 1) < Left(a(i - 1), 1) Then p = a(i - 1): a(i - 1) = a(i): a(i) = p
    Next i
    For i = 1 To Val(m)
    Input #1, y
    c = Split(y)
    b(i, 1) = c(0): b(i, 2) = c(1): b(i, 3) = c(2)
    Next i
    For i = 1 To Val(n)
    For j = 1 To Val(m)
    If a(i) = b(j, 1) Then Print #2, b(j, 1) & " " & b(j, 2) & " " & b(j, 3)
    Next j
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除