2011年11月7日 星期一

99模擬 Problem 3:子字串特徵的判斷

Problem 3:子字串特徵的判斷
此問題為給定某些英文文句,其內容只有「英文、數字、空白及標點符號」,請選手判斷此文
句是否符合某些檢查條件?
子題1(10%):是否文句中存在任何子字串,其以「任一數字」開頭,「任一數字」結尾,中
間存在1 個至3 個大寫字母。
輸入說明:
第1~3 行表示欲檢查之3 句英文文句。
輸出說明:
第1~3 行分別對應第1~3 句檢查文句,若存在符合條件之子字串即輸出「有」,不存在即輸出
「沒有」。
第 5 頁,共 8 頁
輸入範例:【檔名:in-3-1.txt】
Tom Lin’s employee number is A123BSC45.
The price is 45 US dollars.
The machine code is 65K2.
輸出範例:【檔名:out-3-1.txt】
沒有

2 則留言:

  1. Dim X As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Line Input #1, X

    For i = 1 To Len(X)
    A = Mid(X, i, 1)
    If IsNumeric(A) = True Then List1.AddItem i
    Next i


    Ok = False

    For i = 0 To List1.ListCount - 2
    For j = i + 1 To List1.ListCount - 1
    AA = Val(List1.List(i))
    BB = Val(List1.List(j))
    If AA < BB Then If Ok = False Then Ok = check(AA, BB)
    Next j
    Next i
    If Ok = True Then Print #2, "有" Else Print #2, "沒有"
    Loop
    Close #2
    Close #1
    End
    End Sub

    Function check(S, E) As Boolean
    T = 0
    For i = S To E
    A = Mid(X, i, 1)
    If A >= "A" And A <= "Z" Then T = T + 1
    Next i

    If T >= 1 And T <= 3 Then check = True Else check = False
    End Function

    回覆刪除
  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
    Do While Not EOF(1)
    Line Input #1, n
    n = Replace(n, " ", "-")
    Call A1(n)
    Loop
    Close
    Close
    End
    End Sub

    Sub A1(a)

    Dim k As Boolean
    k = False
    ans = 0
    For i = 1 To Len(a)
    m = Mid(a, i, 1)
    If B1(m) = True Then k = Not k
    If k = False Then If ans >= 1 And ans <= 3 Then Print #2, "有": Exit Sub Else ans = 0: If B1(m) = True Then k = True
    If k = True And B1(m) = False And B2(m) = True Then ans = ans + 1
    Next
    Print #2, "沒有"

    End Sub

    Function B1(a)
    B1 = False
    If a >= "0" And a <= "9" Then B1 = True
    End Function

    Function B2(a)
    B2 = False
    If a >= "A" And a <= "Z" Then B2 = True
    End Function

    回覆刪除