2010年3月23日 星期二

竊車問題 (Lostcar)

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

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

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

輸出檔格式 (c:\lostcar\output.txt)
請依車主姓名的順序(英文字母由A~Z)依序印出找到的失竊機車資料。若無法找到任何失竊機車請印出 “Cannot find!”。

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

輸出範例
WIN555 BROWN HO
PIG222 BLACK LIN
SAD321 YELLOW WANG

6 則留言:

  1. Private Sub Form_Load()
    Dim qq(20), ww(100), anscar(10) As String

    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x, y
    For i = 1 To x
    Input #1, p
    qq(i) = p
    Next i
    For i = 1 To y
    Input #1, p
    ww(i) = p
    Next i
    h = 1
    car = 1
    Do Until h > x
    ans = False
    For i = 1 To y
    If qq(h) = Left(ww(i), Len(qq(h))) Then
    anscar(car) = ww(i)
    car = car + 1
    Exit For
    End If
    Next i
    h = h + 1
    Loop
    For i = 1 To car - 1
    For j = i + 1 To car
    If anscar(i) < anscar(j) Then
    temp = anscar(i)
    anscar(i) = anscar(j)
    anscar(j) = temp
    End If
    Next j
    Next i
    For i = 1 To car
    Print #2, anscar(i)
    Next i
    Close #1
    Close #2
    End Sub

    回覆刪除
  2. 網誌管理員已經移除這則留言。

    回覆刪除
  3. 網誌管理員已經移除這則留言。

    回覆刪除
  4. Y揚好,
    這題中,你的最後的排序依據是錯的吧? 他要的是姓名,而不是全部。
    程式的其它,沒有問題。

    回覆刪除
  5. Dim car(20) As String, carID(20) As String, ans(20) 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
    For i = 1 To n
    Input #1, car(i)
    Next i
    For i = 1 To m
    Input #1, carID(i)
    Next i
    k = 1
    For i = 1 To m
    q = Left(carID(i), 6)
    For j = 1 To n
    If q = car(j) Then
    ans(k) = carID(i)
    k = k + 1
    Exit For
    End If
    Next j
    Next i
    For i = 1 To k
    For j = 1 To k
    q1 = Left(ans(i), 1): q2 = Left(ans(j), 1)
    If q1 > q2 Then
    change ans(i), ans(j)
    End If
    Next j
    Next i
    For i = 1 To k
    Print ans(i)
    Next i
    Close #1
    Close #2
    End Sub
    Public Sub change(a, b)
    c = a
    a = b
    b = c
    End Sub

    回覆刪除
  6. Dim n(20) As String, m(100) As String, ansC(20) As String, Dr(20) 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, nn, mm
    If nn > 20 Or mm > 100 Then Exit Sub
    For i = 1 To nn
    Input #1, n(i)
    Next i
    For i = 1 To mm
    Line Input #1, m(i)
    Next i
    x = 1
    For i = 1 To nn
    For j = 1 To mm
    A = Mid(m(j), 1, 6)
    If n(i) = A Then
    ansC(x) = m(j)
    Q = Split(ansC(x))
    Dr(x) = Q(2)
    x = x + 1
    End If
    Next j
    Next i
    x = x - 1
    For i = 1 To x - 1
    s1 = Mid(Dr(i), 1, 1)
    s2 = Mid(Dr(i + 1), 1, 1)
    If s1 > s2 Then
    Call cha(Dr(i), Dr(i + 1))
    Call cha(ansC(i), ansC(i + 1))
    End If
    Next i
    For i = 1 To x
    Print #2, ansC(i)
    Next i
    Close #2
    Close #1
    End Sub
    Public Sub cha(x, y)
    z = x
    x = y
    y = z
    End Sub

    回覆刪除