2012年11月5日 星期一

字串重組

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

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

5 則留言:

  1. Dim str, str1, str2 As String
    Dim check(8) As Boolean
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, str
    strr = Split(str, " ")
    str1 = strr(0)
    str2 = strr(1)
    str = Mid(str1, 1, 3)
    For i = 1 To 8
    If Mid(str2, i, 1) <> "A" And Mid(str2, i, 1) <> "B" And Mid(str2, i, 1) <> "C" And (Len(str) = 8 Or Len(str) < 8) Then str = str & Mid(str2, i, 1)
    Next
    Print #2, str
    Close #2
    Close #1

    回覆刪除
  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
    Input #1, x
    x = Split(x)
    For i = 1 To 3
    y = Mid(x(0), i, 1)
    ans = ans & y
    List1.AddItem (y)
    Next
    For j = 1 To 8
    y = Mid(x(1), j, 1)
    For i = 0 To List1.ListCount - 1
    If y = List1.List(i) Then y = "": Exit For
    Next
    If y <> "" Then ans = ans & y
    Next
    Print #2, ans
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Bob:
    in1:ABCDEFGH EHADBCGF
    out1: ABCEHDGF

    in2: EHADFBGH CEHAGFBC
    out2: EHAEHGF


    復陞:
    in1:ABCDEFGH EHADBCGF
    out1: ABCEHDGF

    in2: EHADFBGH CEHAGFBC
    out2: EHACGFBC


    Bob錯了,復陞對了,還有Bob的程式碼沒貼完整,很嚴重喔!

    回覆刪除
  4. Dim str, str1, str2 As String
    Dim check(8) As Boolean
    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, str
    strr = Split(str, " ")
    str1 = strr(0)
    str2 = strr(1)
    str = Mid(str1, 1, 3)
    For i = 1 To 3
    str2 = Replace(str2, Mid(str, i, 1), "")
    Next
    Print #2, str & Mid(str2, 1, 5)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  5. Dim p 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
    Line Input #1, x
    x = Split(x, " ")
    For i = 1 To 3
    s = Mid(x(0), i, 1)
    a = a & s
    Next
    For i = 1 To Len(x(1))
    p = True
    z = Mid(x(1), i, 1)
    For k = 1 To 3
    If z = Mid(a, k, 1) Then p = False: Exit For
    Next
    If p = True Then t = t & z
    Next
    Print #2, a & t
    Close
    Close
    End
    End Sub

    回覆刪除