2012年8月2日 星期四

密碼分析

密碼分析(cryptanalysis)是指把某個人寫的密文加以分解。這個程序通常會對密文訊息做統計分析。你的任務就是寫一個程式來對密文作簡單的分析。
輸入規範
輸入檔案的第一列有一個正整數n,代表以下有多少列需要分析的密文。接下來的n列,每列含有0或多個字元(可能包含空白字元)
輸出規範
每列包含一個大寫字元(A~Z)和一個正整數。這個正整數代表該字元在輸入檔案中出現的次數。輸入中大小寫(例如:A及a)視為相同的字元。輸出時請按照字元出現的次數由大到小排列,如果有2個以上的字元出現次數相同的話,則按照字元的大小(例如:A在H之前)由小到大排列。
請注意:如果某一字元未出現在輸入檔案中,那它也不應出現在輸出檔案中。
輸入範例(test5.txt)
3
This is a test.
Count me 1 2 3 4 5.
Wow!!!! Is this question easy?
輸出範例(result5.txt)
S 7
T 6
I 5
E 4
O 3
A 2
H 2
N 2
U 2
W 2
C 1
M 1
Q 1
Y 1

2 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim d(26) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")
    Input #1, n
    For k = 1 To n
    Line Input #1, x
    x = UCase(x)
    x = Replace(x, " ", "")
    For i = 1 To Len(x)
    For j = 0 To UBound(a)
    If Mid(x, i, 1) = a(j) Then d(j) = d(j) + 1
    Next
    Next
    Next
    For j = 0 To UBound(a)
    Max = 0
    For i = 0 To UBound(a)
    If d(i) > Max Then Max = d(i): e = i
    Next
    If Max <> 0 Then
    Print #2, a(e) & " " & d(e)
    d(e) = 0
    End If
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  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
    Dim b(26) As Integer
    Input #1, n
    For i = 1 To n
    Line Input #1, aa
    aa = UCase(aa)
    a = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
    For j = 1 To Len(aa)
    For k = 0 To 25
    If Mid(aa, j, 1) = a(k) Then b(k) = b(k) + 1
    Next k, j, i
    c = 0
    For i2 = 0 To 25
    Max = 0
    For g = 0 To 25
    If b(g) > Max Then Max = b(g): c = g
    Next g
    If Max <> 0 Then
    Print #2, a(c) & " " & b(c)
    b(c) = 0
    End If
    Next i2
    Close #2
    Close #1
    End
    End Sub

    回覆刪除