2010年11月8日 星期一

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

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 頁,共 8 頁

子題2(9%):請列出文章中使用那些「A」或「a」開頭的單字?

輸入說明:
第1 行表示文章的行數(行數至多10 行,每行最多120 個字),第2 行開始為文章的內容。
輸出說明:
依單字在文章中出現之順序,輸出開頭為「A」或「a」的單字。輸出的單字大小寫需保持與
原文章相同,而每行只輸出1 個單字。若同一單字多次出現在文章中,只有該單字在文章中
首次出現時才被輸出,第2 次或以後出現時均不再輸出。
輸入範例:【檔名:in-1-2.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-2.txt】
ask
a
apple
are
And
As
activity
at
average

5 則留言:

  1. Dim Sym(26) As Long
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in-1-1.txt" For Input As #1
    Open App.Path & "\out-1-1.txt" For Output As #2
    strAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Input #1, KKK
    For kk = 1 To KKK
    Line Input #1, strQ
    For i = 1 To Len(strQ)
    A = UCase(Mid(strQ, i, 1))
    If InStr(1, strAL, A) <> 0 Then
    Sym(InStr(1, strAL, A)) = Sym(InStr(1, strAL, A)) + 1
    End If
    Next i
    Next kk
    Print #2, "(A," & Sym(1) & ")" & "(B," & Sym(2) & ")" & "(C," & Sym(3) & ")" & "(D," & Sym(4) & ")" & "(E," & Sym(5) & ")"
    Print #2, "(F," & Sym(6) & ")" & "(G," & Sym(7) & ")" & "(H," & Sym(8) & ")" & "(I," & Sym(9) & ")" & "(J," & Sym(10) & ")"
    Print #2, "(K," & Sym(11) & ")" & "(L," & Sym(12) & ")" & "(M," & Sym(13) & ")" & "(N," & Sym(14) & ")" & "(O," & Sym(15) & ")"
    Print #2, "(P," & Sym(16) & ")" & "(Q," & Sym(17) & ")" & "(R," & Sym(18) & ")" & "(S," & Sym(19) & ")" & "(T," & Sym(20) & ")"
    Print #2, "(U," & Sym(21) & ")" & "(V," & Sym(22) & ")" & "(W," & Sym(23) & ")" & "(X," & Sym(24) & ")" & "(Y," & Sym(25) & ")"
    Print #2, "(Z," & Sym(26) & ")"
    Close #2
    Close #1

    Open App.Path & "\in-1-2.txt" For Input As #1
    Open App.Path & "\out-1-2.txt" For Output As #2
    strAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Input #1, KKK
    For kk = 1 To KKK
    Line Input #1, strQ
    AA = Split(strQ)
    For i = 0 To UBound(AA)
    ans = True
    A = UCase(Mid(AA(i), 1, 1))
    If A = "A" Then
    For k = 0 To Lst.ListCount
    If Lst.List(k) = AA(i) Then ans = False
    Next k
    If ans = True Then Print #2, AA(i): Lst.AddItem AA(i)
    End If
    Next i
    Next kk
    Close #2
    Close #1
    End
    End Sub


    BY 小白

    回覆刪除
  2. 1-1

    Private Sub Form_Load()
    Dim nword(26) As Integer
    Me.Hide
    Open App.Path & "\in-1-1.txt" For Input As #1
    Open App.Path & "\out-1-1.txt" For Output As #2
    Input #1, x
    For i = 1 To x
    Line Input #1, step
    For j = 1 To Len(step)
    k = (Asc(LCase(Mid(step, j, 1))) - 96)
    If k <= 26 And k > 0 Then
    nword(k) = nword(k) + 1
    End If
    Next j
    Next i
    n = 1
    For i = 1 To 26
    Print #2, "(" & Chr(i + 64) & "," & nword(i) & ")";
    n = n + 1
    If n = 6 Then n = 1: Print #2,
    Next i
    Close #1
    Close #2
    End
    End Sub

    by Yung

    回覆刪除
  3. 1-2

    Private Sub Form_Load()
    Dim ans As String
    Me.Hide
    Open App.Path & "\in-1-2.txt" For Input As #1
    Open App.Path & "\out-1-2.txt" For Output As #2
    Input #1, x
    For i = 1 To x
    Line Input #1, step
    step = Replace(step, ",", "")
    step = Replace(step, ".", "")
    k = Split(step)
    For J = 0 To UBound(k)
    If LCase(Left(k(J), 1)) = "a" Then
    k(J) = " " & k(J) & " "
    If InStr(ans, k(J)) = 0 Then
    Print k(J)
    ans = ans & " " & k(J)
    End If
    End If
    Next J
    Next i
    Close #1
    Close #2
    End
    End Sub

    BY Yung

    回覆刪除
  4. Private Sub Form_Activate()
    Dim eng(65 To 90)
    Open "in-1-1.txt" For Input As #1
    Input #1, q
    Do While Not EOF(1)
    Line Input #1, q1
    q1 = UCase(q1) '轉大寫
    For i = 1 To Len(q1)
    m = Asc(Mid(q1, i, 1)) '抓字母asc
    If m >= 65 And m <= 90 Then
    eng(m) = eng(m) + 1
    End If
    Next i
    Loop
    Close #1
    Open "out-1-1.txt" For Output As #2
    For i = 65 To 90
    k = k + 1
    If eng(i) = "" Then eng(i) = 0 '丟空值
    Print #2, "("; Chr(i); ","; eng(i); ")";
    If k Mod 5 = 0 Then Print #2,
    Next i
    Close #2
    End Sub

    回覆刪除
  5. ' 1-2
    Private Sub Form_Activate()
    Dim a(1200) '所有A字母的單字記錄區
    Dim b(1200)
    Dim sp() As String
    Open "in-1-2.txt" For Input As #1
    Input #1, q
    If q > 10 Then '判斷是否超過規定之行數
    MsgBox "哦哦!行數超過了!!請重新檢查輸入檔", vbExclamation, "VB提醒員"
    GoTo emd '
    End If
    Do While Not EOF(1)
    Line Input #1, q1
    'Print q1
    For i = 1 To Len(q1)
    If Mid(q1, i, 1) <> " " And Mid(q1, i, 1) <> "," And Mid(q1, i, 1) <> "." Then
    r$ = r$ & Mid(q1, i, 1)
    If i = Len(q1) Then
    m = m + 1
    b(m) = r$
    r$ = ""
    End If
    Else
    m = m + 1
    b(m) = r$
    r$ = ""
    End If
    Next i
    For i = 1 To m
    If Mid(b(i), 1, 1) = "A" Or Mid(b(i), 1, 1) = "a" Then '記錄"A"或"a"的單字
    k = k + 1
    a(k) = b(i)
    End If
    Next i
    Loop
    Close #1
    For i = 1 To k - 1
    For j = i + 1 To k
    If a(i) = a(j) Then
    a(j) = ""
    End If
    Next j
    Next i
    For i = 1 To k - 1
    For j = i + 1 To k - 1
    If a(j) = "" Then
    a(j) = a(j + 1)
    a(j + 1) = ""
    End If
    Next j
    Next i
    Open "out-1-2.txt" For Output As #2
    For i = 1 To k
    If a(i) <> "" Then
    Print #2, a(i)
    End If
    Next i
    Close #2
    emd:
    End Sub

    回覆刪除