2011年11月7日 星期一

99模擬 Problem 1:文章的字母統計及單字的分離

Problem 1:文章的字母統計及單字的分離
選手請由輸入檔讀入一篇英文文章(文章內容包括「大、小寫英文」、「空白」及「標點符號」,
沒有任何「全型」字母或符號),並作以下的統計:
子題1(9%): 若不區別英文字母大小寫,請統計26 個英文字母A~Z在文章中出現的次數?
(只統計英文字母,其他如空白、標點符號等皆不統計。)

輸入說明:
第1 行表示文章的行數(行數至多10 行,每行最多120 個字),第2 行開始為文章的內容。
輸出說明:
第1 行分別列出字母A、B、C、D、E 及其統計次數,第2 行列出字母F、G、H、I、J 及其
統計次數,每行輸出5 個字母及統計,以此類推;但最後一行只列出字母Z 及其統計次數。
英文字母以「大寫」輸出,每個字母及其統計次數印在1 組小括號中,並由逗號分隔。
輸入範例:【檔名:in-1-1.txt】
4
Just ask a Chinese fruit farmer who now has to pay people to pollinate apple trees because there are no
longer enough bees to do the job. And it's not just the number of bees that are rapidly dwindling. As a
direct result of human activity, species are becoming extinct at a rate 1,000 times greater than the natural
average.
輸出範例:【檔名:out-1-1.txt】
(A, 27) (B, 6) (C, 7) (D, 6) (E, 38)
(F, 4) (G, 6) (H, 11) (I, 14) (J, 3)
(K, 1) (L, 9) (M, 5) (N, 16) (O, 16)
(P, 8) (Q, 0) (R, 18) (S, 15) (T, 28)
(U, 9) (V, 2) (W, 3) (X, 1) (Y, 3)
(Z, 0)

2 則留言:

  1. Dim S(90) 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
    Input #1, t
    For i = 1 To t
    Line Input #1, n
    n = UCase(n)
    Call A1(n)
    Next
    Call A2
    Close
    Close
    End
    End Sub

    Sub A1(a)
    For i = 1 To Len(a)
    m = Mid(a, i, 1)
    S(Asc(m)) = S(Asc(m)) + 1
    Next
    End Sub

    Sub A2()
    x = 0
    For i = 65 To 90
    If x = 5 Then x = 0: Print #2, ""
    Print #2, "(" & Chr(i) & ", " & S(i) & ")";
    x = x + 1
    Next
    End Sub

    回覆刪除
  2. Dim X(26) 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
    Input #1, N
    For ii = 1 To N
    Line Input #1, Y
    Y = UCase(Y)

    For i = 1 To Len(Y)
    A = Mid(Y, i, 1)
    If A >= "A" And A <= "Z" Then X(Asc(A) - 64) = X(Asc(A) - 64) + 1
    Next i

    Next ii

    T = 0
    For i = Asc("A") To Asc("Z")
    Print #2, "(" & Chr(i) & "," & X(i - 64) & ")";
    T = T + 1
    If T Mod 5 = 0 Then Print #2,
    Next i

    Close #2
    Close #1
    End
    End Sub

    回覆刪除