2011年11月7日 星期一

99模擬 Problem 3-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】
沒有

2 則留言:

  1. 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
    Call A1(n)
    Loop
    Close
    Close
    End
    End Sub

    Sub A1(a)
    For i = 1 To Len(a)
    m = Mid(a, i, 1)
    If m = "A" Or m = "B" Or m = "E" And Len(a) - i >= 10 Then If A2(Mid(a, i, 10)) = True Then Print #2, "有": Exit Sub
    Next
    Print #2, "沒有"
    End Sub

    Function A2(a)
    m = Mid(a, 1, 1)
    Select Case m
    Case "A": ans = 1
    Case "B": ans = 10
    Case "E": ans = 37
    End Select
    Dim AAA
    AAA = Split("X X 8 7 6 5 4 3 2 1 1")
    For i = 2 To 10
    m = Mid(a, i, 1)
    ans = ans + Val(m) * Val(AAA(i))
    Next
    If ans Mod 10 = 0 Then A2 = True Else A2 = 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, X
    List1.Clear
    For i = 1 To Len(X)
    If check_1(Mid(X, i, 10)) = True Then List1.AddItem Mid(X, i, 10)
    Next i

    OK = False
    For i = 0 To List1.ListCount - 1
    If check_2(List1.List(i)) = True Then OK = True
    Next i

    If OK = True Then Print #2, "有" Else Print #2, "沒有"

    Loop
    Close #2
    Close #1
    End
    End Sub

    Function check_1(A) As Boolean '判斷是否為正確身分證字號
    P = False

    If Len(A) = 10 Then
    B = Left(A, 1)
    sex = Val(Mid(A, 2, 1))
    If (B = "A" Or B = "B" Or B = "E") And (sex = 1 Or sex = 2) And IsNumeric(Right(A, 9)) Then P = True
    End If

    check_1 = P
    End Function

    Function check_2(A) As Boolean
    P = False
    B = Left(A, 1)
    Select Case B
    Case "A": A = Replace(A, "A", 10)
    Case "B": A = Replace(A, "B", 11)
    Case "E": A = Replace(A, "E", 14)
    End Select
    total = Val(Mid(A, 1, 1)) + Val(Mid(A, 11, 1))

    T = 9
    For i = 2 To 10
    C = Val(Mid(A, i, 1))
    total = total + C * T
    T = T - 1
    Next i
    If total Mod 10 = 0 Then P = True
    check_2 = P
    End Function

    回覆刪除