2011年1月10日 星期一

字串重組

請輸入兩組字串,每組長度為八個字元。請注意,字串必須是由A、B、C、D、E、F、G、H這些英文字母所組合的,且每個英文字母再一個字串中限制只能出現一次。請設計一個城市,利用這兩組輸入的字串,組合另一個新的字串輸出。組合的法則為:新字串的前三個字元是從第一組輸入字串的前三個字元而來,後五個字原則是由第二組輸入字串從頭依序遞補上來的。遞補的條件為:遞補的字元必須跟第一組的前三個字元不同。

輸入範例:
ABCDEFGH EHADBCGF
輸出範例:
ABCEHDGF

參照 http://chscvb.blogspot.com/2010/02/20100202.html

8 則留言:

  1. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Me.Hide
    Input #1, a
    a1 = Left(a, 3)
    a2 = Right(a, 8)
    ans = a1
    For i = 1 To 8
    tmp = Mid(a2, i, 1)
    If InStr(a1, tmp) = 0 Then ans = ans & tmp
    If Len(ans) = 8 Then Exit For
    Next

    Print #2, ans

    Close
    Close
    End
    End Sub


    -----
    今天的第2題 =D

    回覆刪除
  2. arro好,
    程式OK。
    instr(a,b)這個字串函數可以用之外,也可以試試replace(a,b)這個字串函數哦。
    熊掌

    回覆刪除
  3. Dim AR As String
    Dim A1 As String
    Dim A2 As String
    Dim ANew As String

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, AR
    Close #1
    A1 = Left(AR, 8)
    A2 = Right(AR, 8)
    ANew = Left(A1, 3)

    Do Until Len(ANew) = 8
    For i = 1 To 8
    If InStr(ANew, Mid(A2, i, 1)) = 0 Then ANew = ANew & Mid(A2, i, 1): Exit For
    Next i
    Loop

    Open App.Path & "\out.txt" For Output As #2
    Print #2, ANew
    Close #2
    End
    End Sub

    回覆刪除
  4. 佑好,那個條件迴圈有問題,直接改成一個if條件,放在計次迴圈內就好。
    熊掌

    回覆刪除
  5. Dim AR As String
    Dim A1 As String
    Dim A2 As String
    Dim ANew As String

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, AR
    Close #1
    A1 = Left(AR, 8)
    A2 = Right(AR, 8)
    ANew = Left(A1, 3)


    For i = 1 To 8
    If InStr(ANew, Mid(A2, i, 1)) = 0 Then ANew = ANew & Mid(A2, i, 1)
    Next i


    Open App.Path & "\out.txt" For Output As #2
    Print #2, ANew
    Close #2
    End
    End Sub

    看來我是多寫的^^~謝謝

    回覆刪除
  6. For i = 1 To 8
    If InStr(ANew, Mid(A2, i, 1)) = 0 Then ANew = ANew & Mid(A2, i, 1)
    if Len(ANew) = 8 then exit for
    Next i

    老師的意思這樣才對:D

    回覆刪除
  7. 佑好,
    你的兩個程式都對,因為這一題的限制,一定會變成8個字,所以前一個程式就可以了。
    熊掌

    回覆刪除
  8. 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
    Dim s
    s = Split(n)
    Call A1(s(0), s(1))
    Close
    Close
    End
    End Sub

    Sub A1(A, B)
    Dim ans As String, Q, t
    ans = Left(A, 3)
    Q = ans
    t = 0
    For i = 1 To Len(B)
    m = Mid(B, i, 1)
    If InStr(Q, m) = 0 Then ans = ans & m: t = t + 1
    If t = 5 Then Exit For
    Next
    Print #2, ans
    End Sub


    2:48

    回覆刪除