2012年11月20日 星期二

子字串特徵的判斷-2

子題2(10%):是否文句中存在任何子字串,其為「合理」的「台北市」、「台中市」或「高雄市」身份證字號。
註:
(1) 身份證字號第1 碼為地區碼,「台北市」以「A」開頭、「台中市」以「B」開頭、「高雄市」以「E」開頭。

(2) 身份證字號的第2 碼為「性別」碼,其值應為「1」或「2」。
(3) 若將身份證字號的「地區碼」改為2 碼數字,其中「A」改為「10」、「B」改為「11」、「E」改為「14」,其後併入原身份證後9 碼數字,成為1 個新的11 位數字碼。將此數字碼由左至右分別乘以「1、9、8、7、6、5、4、3、2、1、1」,其相乘後的總和應可被10 整除。

輸入說明:
第1~3 行表示欲檢查之文句。
輸出說明:
若存在合理的身份證字號即輸出「有」,不存在即輸出「沒有」。
輸入範例:【檔名:in-3-2.txt】
His ID number is A120441768.
Her ID number is B272857734.
Their ID numbers are E286585485, E282467997, and E195445887.

輸出範例:【檔名:out-3-2.txt】

沒有

4 則留言:

  1. Dim strr As String
    Dim ans As Integer
    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
    Line Input #1, strr
    For i = 97 To 122
    strr = Replace(strr, Chr(i), "")
    Next
    strr = Replace(strr, "T", "")
    strr = Replace(strr, "H", "")
    strr = Replace(strr, "I", "")
    strr = Replace(strr, "D", "")
    strr = Replace(strr, " ", "")
    strr = Replace(strr, ".", "")
    strr = Replace(strr, "A", "10")
    strr = Replace(strr, "B", "11")
    strr = Replace(strr, "E", "14")
    ok = Split(strr, ",")
    For ai = 0 To UBound(ok)
    ans = 0
    If Val(Mid(ok(ai), 3, 1)) = 1 Or Val(Mid(ok(ai), 3, 1)) = 2 Then
    For i = 9 To 2 Step -1
    ans = ans + Val(Mid(ok(ai), 11 - i, 1)) * i
    Next
    ans = ans + Val(Mid(ok(ai), 1, 1)) + Val(Mid(ok(ai), 11, 1)) + Val(Mid(ok(ai), 10, 1))
    If ans Mod 10 = 0 Then Print #2, "有": Exit For
    If ans Mod 10 <> 0 And ai = UBound(ok) Then Print #2, "沒有"
    Else
    If ai = UBound(ok) Then Print #2, "沒有"
    End If
    Next

    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. 作者已經移除這則留言。

    回覆刪除
  3. Dim str1 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
    Do
    str1 = False
    Line Input #1, n
    For i = 1 To 1000
    If Mid(n, i, 1) = "A" Or Mid(n, i, 1) = "B" Or Mid(n, i, 1) = "E" Then
    ans = Mid(n, i, 1)
    For j = i + 1 To i + 9
    a = Mid(n, j, 1)
    If Asc(a) < 48 Or Asc(a) > 57 Then
    str1 = False
    Exit For
    Else
    ans = ans & Mid(n, j, 1)
    str1 = True
    End If
    Next
    ans = Replace(ans, "A", "10")
    ans = Replace(ans, "B", "11")
    ans = Replace(ans, "E", "14")
    ans1 = Mid(ans, 1, 1) * 1
    For y = 9 To 1 Step -1
    ans1 = ans1 + Mid(ans, (11 - y), 1) * y
    Next
    ans1 = ans1 + Mid(ans, 11, 1) * 1
    If ans1 Mod 10 = 0 Then
    str1 = True
    Exit For
    Else
    str1 = False
    End If
    ans = 0
    ans1 = 0
    End If
    Next
    If str1 Then
    Print #2, "有"
    Else
    Print #2, "沒有"
    End If
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Dim BG 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
    Do Until EOF(1)
    Line Input #1, x
    x = Replace(x, " ", "")
    x = Replace(x, ".", "")
    For i = 97 To 122
    x = Replace(x, Chr(i), "")
    Next
    For i = 67 To 90
    If i <> 69 Then x = Replace(x, Chr(i), "")
    Next
    x = Replace(x, "A", "10")
    x = Replace(x, "B", "11")
    x = Replace(x, "E", "14")
    s = Split(x, ",")
    For i = 0 To UBound(s)
    For k = 1 To Len(s(i))
    g = Mid(s(i), k, 1)
    If IsNumeric(g) = False Then s(i) = Replace(s(i), g, "")
    If Len(s(i)) <> 11 Then Print #2, "沒有": Exit Do
    Next
    ans = 0
    If Val(Mid(s(i), 3, 1)) = 1 Or Val(Mid(s(i), 3, 1)) = 2 Then BG = True
    If BG = True Then
    For g = 9 To 1 Step -1
    ans = ans + Val(Mid(s(i), 11 - g, 1)) * g
    Next g
    ans = ans + Val(Left(s(i), 1)) + Val(Right(s(i), 1))
    End If
    If ans Mod 10 = 0 Then Print #2, "有": Exit For
    If ans Mod 10 <> 0 And i = UBound(s) Then
    Print #2, "沒有"
    Else
    If i = UBound(s) Then Print #2, "沒有"
    End If
    Next
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除