輸入一篇英文文章,計算文中任兩英文字母接連出現的頻率,亦即@A,A@,AA,AB,
AC,﹒﹒﹒﹒,AZ,@B,B@,BA,BB,BC,﹒﹒﹒﹒
BZ,﹒﹒﹒﹒﹒﹒﹒﹒﹒﹒﹒,ZZ等出現的字數。
其中@A代表A為一英文單字的第一個字母;A@代表A為一英文單字的最後一個字母,餘類推。按其頻率之高低排序後,將頻率不為零的依序列印出來。
[例] 輸入:
THIS IS HIS BOOK
列印出的結果為:
IS:3 S@:3 HI:2 @B:1
@H:1 @I:1 @T:1 BO:1
k@:1 OK:1 OO:1 TH:1
'------------錯誤的程式碼
Private Sub Command1_Click()
Dim a()
s = "THIS IS HIS BOOK" '輸入
If Left(s, 1) <> " " Then s = " " & s
If Right(s, 1) <> " " Then s = s & " "
ReDim a(1, Len(s) - 1)
t = 0
For i = 1 To Len(s) - 1 '本題的中心
For j = 1 To i - 1
If a(0, j) = Replace(Mid(s, i, 2), " ", "@") Then Exit For
Next j
If j = i Or i = 1 Then
t = t + 1
a(0, t) = Replace(Mid(s, i, 2), " ", "@")
a(1, t) = (Len(s) - Len(Replace(s, Mid(s, i, 2), ""))) / 2
End If
Next i
For i = 1 To t - 1 '氣泡排序
For j = 1 To t - i
If a(1, j) < a(1, j + 1) Then
Call swap(a(1, j), a(1, j + 1))
Call swap(a(0, j), a(0, j + 1))
End If
Next j
Next i
For i = 1 To t '輸出
Print a(0, i) & ":" & a(1, i)
Next i
End Sub
Sub swap(a, b) '交換
t = a
a = b
b = t
End Sub
2006-12-01 06:06:27 · 1 個解答 · 發問者 ? 1 in 電腦與網際網路 ➔ 程式設計
wjs 大 我輸入 aa bb cc 就會錯耶....
2006-12-01 17:11:10 · update #1
a@ 2
b@ 2
c@ 2
aa 2
bb 2
cc 2
我想要的結果
2006-12-04 03:28:05 · update #2
Private Sub Command1_Click() Dim A$, T$, K$, S$(), N%(), I%, J% A = "THIS IS HIS BOOK" A = Replace("@" & A & "@", " ", "@") T = " " For I = 1 To Len(A) - 1 K = Mid$(A, I, 2) If InStr(T, " " & K & " ") = 0 Then ReDim Preserve S(J), N(J) S(J) = K: N(J) = UBound(Split(A, K)) J = J + 1: T = T & K & " " End If Next For I = 0 To UBound(N) - 1 For J = I To UBound(N) If N(I) < N(J) Then T = N(I): N(I) = N(J): N(J) = T T = S(I): S(I) = S(J): S(J) = T ElseIf N(I) = N(J) Then If S(I) > S(J) Then T = S(I): S(I) = S(J): S(J) = T End If Next Next For I = 0 To UBound(N) Print S(I); ":"; N(I) NextEnd Sub
2006-12-02 11:46:40 補充:
A = "aa bb cc"輸出:@a:1 @b:1 @c:1 a@:1 aa:1 b@:1 bb:1 c@:1 cc:1應該沒錯吧?你的條件不是就是數字由大到小,文字由小到大嗎?
2006-12-04 12:26:52 補充:
搞不懂,我想我被你打敗了XDaa bb cc>aa & bb & cc 各只出現1次, 怎麼你要的結果變成2次?那跟你的題目又不同了.
2006-12-01 12:49:26 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋