2011年8月12日 星期五

文章的字母統計及單字的分離(99模擬) -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)

5 則留言:

  1. 用了list來記錄

    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 i = 0 To 26
    List1.AddItem 0
    Next i
    A = ""

    Do While Not EOF(1)
    Line Input #1, X
    For i = 1 To Len(X)
    Y = UCase(Mid(X, i, 1))
    If Y >= "A" And Y <= "Z" Then
    A = A & Y
    End If
    Next i
    Loop

    For i = 1 To Len(A)
    B = Asc(Mid(A, i, 1)) - 64
    List1.List(B) = Val(List1.List(B)) + 1
    Next i

    C = 1
    For i = 1 To 5
    ans = ""
    For j = 1 To 5
    ans = ans & "(" & Chr(C + 64) & ", " & List1.List(C) & ") "
    C = C + 1
    Next j
    Print #2, ans
    Next i

    Print #2, "(" & Chr(C + 64) & ", " & List1.List(C) & ") "


    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. 佑好,
    程式可以正確執行。
    但是,還是太慢了,多了些不必要的東西。
    1.這題只是計次問題,用陣列(dim c(26) )就好了,不用再特別用到listbox了。
    2.直接在A = A & Y這兒去改成
    b=Asc(y)-64
    c(b)=c(b)+1
    (或是寫成1行也行),你將它全部接成1個字串,再1個1個將它分解出來,不是多此一舉嗎

    回覆刪除
  3. 熊掌好,

    真的是太多不要的東西了。

    Private Sub Form_Load()
    Dim N(26) As Integer
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, Q

    A = ""

    Do While Not EOF(1)
    Line Input #1, X

    X = UCase(X)
    For i = 1 To Len(X)
    B = Asc(Mid(X, i, 1)) - 64
    If B <= 26 And B >= 0 Then N(B) = N(B) + 1
    Next i

    Loop

    C = 1
    For i = 1 To 5
    ans = ""
    For j = 1 To 5
    ans = ans & "(" & Chr(C + 64) & ", " & N(C) & ") "
    C = C + 1
    Next j
    Print #2, ans
    Next i

    Print #2, "(" & Chr(C + 64) & ", " & N(C) & ") "


    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. 佑好,
    C = 1
    For i = 1 To 5
    ans = ""
    For j = 1 To 5
    ans = ans & "(" & Chr(C + 64) & ", " & N(C) & ") "
    C = C + 1
    Next j
    Print #2, ans
    Next i

    Print #2, "(" & Chr(C + 64) & ", " & N(C) & ") "
    ****************************
    用更簡單的方式
    ***************************
    for i = 1 to 26
    Print #2, "(" & Chr(i + 64) & ", " & N(C) & ") ";
    if i mod 5 = 0 then print #2,""
    next i

    回覆刪除
  5. 熊掌好,


    剛剛試了一下
    在 print #, 後面加; , 分隔
    果真與print於表單一樣的效果

    又學到一招了
    謝老師

    回覆刪除