2010年10月8日 星期五

密碼驗證與擷取

內容 : 
  小明參加寒假的冬令營,闖關活動中有一關是猜密碼遊戲。首先關主會給闖關者一組阿拉伯數字組成的字串,闖關者要先判斷它是否是不是真正藏有密碼的字串,藏有密碼的字串有下列特性:
  1.它由阿拉伯數字1,2,3,4,5,6,7,8,9組成,且長度介於10至30字元之間。
  2.它必須是迴文(palindrome)字串,也就是不論從左到右或從右到左都是一樣的字串,它可以是奇數或偶    數個字元。例如:12345654321、3344554433等。
  3.字串中兩兩接連的數字之間,後面的數字一定不會大於前面的數兩唄。例如:22221512222 就不是一個    藏有密碼的字串,因為'5'大於'1'的兩倍。
  當闖關者確定藏有密碼時,只要將字串中的偶數字元挑出,就可以得到密碼順利過關了。舉個例子來說:若輸入的字串是 42643734624 ,雖然它是一個迴文字串,但是字串中的第三個數字'6'大於第二個數字'2'的二倍,第六個數字'7'也大於第五個數字'3'的二倍,所以它就不是一個藏有的密碼字串。另外,若輸入的字串是 423435534324 ,它不但是一個迴文字串,且兩兩接連的數字之間,後面的數字一定不會大於前面數字的二倍,所以它就是一個藏有的密碼字串,而藏在此字串中的密碼就是 424424(密碼由字串中偶數的數字所組成,如此例中畫底線的部分→423435534324)。你可以寫一個程式協助小明快速的解出密碼嗎?
輸入說明 :
輸入字串只有一行,由阿拉伯數字1,2,3,4,5,6,7,8,9組成,長度介於10至30字元之間。
輸出說明 :
第一行輸出密碼。輸入字串並非藏有密碼的字串,輸出"INCORRECT"訊息。輸入字串若為藏有密碼的字串,但字串中並無包含偶數,輸出"0"。
範例輸入 :help
輸入範例一: 154321123451 
輸入範例二: 123456777654321
範例輸出 :
輸出範例一: INCORRECT 
輸出範例二: 246642

4 則留言:

  1. Dim strQ As String, ans As Boolean, L As Integer, AnsSt 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
    Line Input #1, strQ
    ans = True
    Call P(strQ, ans)
    If ans = True Then
    Call T(strQ)
    Else
    Print #2, "INCORRECT"
    End If
    Close #2
    Close #1
    End
    End Sub

    Public Sub P(ByVal st As String, ByRef ans As Boolean)
    L = Len(st)
    If L > 30 Or L < 10 Then ans = False: Exit Sub
    k = 1
    Do Until k = L
    A1 = Val(Mid(st, k, 1))
    A2 = Val(Mid(st, L - k + 1, 1))
    A3 = Val(Mid(st, k + 1, 1))
    If A1 = 0 Then ans = False: Exit Sub
    If A2 <> A1 Then ans = False: Exit Sub
    If A3 > A1 * 2 Then ans = False: Exit Sub
    k = k + 1
    Loop
    End Sub


    Public Sub T(ByVal st As String)
    AnsSt = ""
    L = Len(st)
    For i = 1 To L
    x = Val(Mid(st, i, 1))
    If x Mod 2 = 0 Then
    AnsSt = AnsSt & x
    End If
    Next i
    If AnsSt = "" Then AnsSt = 0
    Print #2, AnsSt
    End Sub



    BY 小白

    回覆刪除
  2. Dim ans As Boolean
    Dim k As String
    Dim kk As String
    Dim num(50) As Integer
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    ans = True
    For i = 1 To Len(x) \ 2
    If Mid(x, i, 1) <> Mid(x, Len(x) - i + 1, 1) Then
    ans = False
    Else
    num(i) = Val(Mid(x, i, 1))
    If ((num(i) > num(i - 1) * 2) Or (num(i) < num(i - 1))) And i <> 1 Then
    ans = False
    Exit For
    Else
    If num(i) Mod 2 = 0 Then k = k & num(i): kk = num(i) & kk
    End If
    End If
    Next i
    If ans Then
    Print #2, k; kk
    Else
    Print #2, "INCORRECT"
    End If
    Close #2
    Close #1
    End Sub

    BY 阿揚

    回覆刪除
  3. Dim strQ As String, ans As Boolean, L As Integer, AnsSt 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
    Line Input #1, strQ
    ans = True
    Call ch1(strQ, ans)
    If ans = True Then Call ch2(strQ, ans)
    If ans = True Then Call ch3(strQ, ans)
    If ans = True Then
    Call T(strQ)
    Else
    Print #2, "INCORRECT"
    End If
    Close #2
    Close #1
    End
    End Sub

    Public Sub T(ByVal st As String)
    AnsSt = ""
    L = Len(st)
    For i = 1 To L
    x = Val(Mid(st, i, 1))
    If x Mod 2 = 0 Then
    AnsSt = AnsSt & x
    End If
    Next i
    If AnsSt = "" Then AnsSt = 0
    Print #2, AnsSt
    End Sub

    Public Sub ch1(ByVal st As String, ByRef ans As Boolean)
    L = Len(st)
    If L > 30 Or L < 10 Then ans = False: Exit Sub
    k = 1
    Do Until k = L
    A = Val(Mid(st, k, 1))
    If A = 0 Then ans = False: Exit Sub
    k = k + 1
    Loop
    End Sub

    Public Sub ch2(ByVal st As String, ByRef ans As Boolean)
    L = Len(st)
    k = 1
    Do Until k = L
    A1 = Val(Mid(st, k, 1))
    A2 = Val(Mid(st, L - k + 1, 1))
    If A2 <> A1 Then ans = False: Exit Sub
    k = k + 1
    Loop
    End Sub

    Public Sub ch3(ByVal st As String, ByRef ans As Boolean)
    L = Len(st)
    k = 1
    Do Until k = L
    A1 = Val(Mid(st, k, 1))
    A2 = Val(Mid(st, k + 1, 1))
    If A2 > A1 * 2 Then ans = False: Exit Sub
    k = k + 1
    Loop
    End Sub


    BY 小白

    回覆刪除
  4. 用小白的第二個程式,思緒會清楚些,是好的。

    回覆刪除