請輸入兩組字串,每組長度為八個字元。請注意,字串必須是由A、B、C、D、E、F、G、H這些英文字母所組合的,且每個英文字母再一個字串中限制只能出現一次。請設計一個城市,利用這兩組輸入的字串,組合另一個新的字串輸出。組合的法則為:新字串的前三個字元是從第一組輸入字串的前三個字元而來,後五個字原則是由第二組輸入字串從頭依序遞補上來的。遞補的條件為:遞補的字元必須跟第一組的前三個字元不同。
輸入範例:
ABCDEFGH EHADBCGF
輸出範例:
ABCEHDGF
參照 http://chscvb.blogspot.com/2010/02/20100202.html
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 a, c(8), d(3) As String
Input #1, a
Print #2, Left(a, 3);
b = Right(a, 8)
For i = 1 To 8
c(i) = Mid(b, i, 1)
Next
For i = 1 To 3
d(i) = Mid(a, i, 1)
Next
For y = 1 To 8
If d(1) <> c(y) Then
If d(2) <> c(y) Then
If d(3) <> c(y) Then Print #2, c(y) & "";
End If
End If
Next
Close
Close
End
End Sub
小冰好,
回覆刪除程式正確。
可以試試學長們很喜歡用的replace,更直接。
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, s0
For i = 1 To 8
s1 = s1 & Mid(s0, i, 1)
Next i
For j = 10 To 17
s2 = s2 & Mid(s0, j, 1)
Next j
s3 = Left(s1, 3)
For k = 1 To 8
If InStr(s3, Mid(s2, k, 1)) = 0 Then s3 = s3 & Mid(s2, k, 1)
Next k
Print #2, s3
Close #2
Close #1
End
End Sub
作者已經移除這則留言。
回覆刪除Private Sub Form_Load()
回覆刪除Dim str1, str2,a(8) As String
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
b = Split(n)
str1 = Left(b(0), 3)
For i = 1 To 8
a(i) = Mid(b(1), i, 1)
If InStr(b(0), a(i)) > 3 Then str2 = str2 & a(i)
If Len(str2) = 5 Then Exit For
Next i
Print #2, str1 & str2
Close #1
Close #2
End
End Sub