求助:VB 统计次数
第一组数02 05 06 12 14 28 04 06 12 30 31 32 02 08 13 28 29 30 01 02 05 16 20 26 01 07 08 12 16 21第二组数01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
统计第一组数出现01-33的各种次数正确答案应该如下:
〖00次〗03 09 10 11 15 17 18 19 22 23 24 25 27 33 (共14个)
〖01次〗04 07 13 14 20 21 26 29 31 32 (共10个)
〖02次〗01 05 06 08 16 28 30 (共7个)
〖03次〗02 12 (共2个) --------------------编程问答--------------------
那第二个数组是干吗的呢? --------------------编程问答-------------------- --------------------编程问答-------------------- 可以利用 ListBox 统计:
Option Explicit
Private Declare Function SendMessagebyString Lib _
"user32" Alias "SendMessageA" (ByVal hWND As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As String) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2 '在 ListBox 中精确查找
Dim strSource As String, strItem() As String
Dim i As Long, n As Long
strSource = "02 05 06 12 14 28 04 06 12 30 31 32 02 08 13 28 29 30 01 02 05 16 20 26 01 07 08 12 16 21"
List1.Clear
strItem = Split(strSource, " ")
For i = 0 To Ubound(strItem)
n = SendMessagebyString(List1.hWnd, LB_FINDSTRINGEXACT, -1, strItem(i))
If n = -1 Then '没有匹配列表项
List1.AddItem strItem(i)
List1.ItemData(List1.NewIndex) = 1
Else
List1.ItemData(n) = List1.ItemData(n) + 1
End If
Next i
For i = 0 To List1.ListCount - 1
Debug.Print List1.List(i), List1.ItemData(i)
Next i
列表项就是组内元素,对应的 ItemData 是出现次数。 --------------------编程问答-------------------- 要得到你的按次数汇总的字符串,直接对组成员扫描也很直接:
--------------------编程问答-------------------- 利用 ListBox 查找组 2 元素在组1 中出现次数的过程内代码如下:
Dim strSource As String, strItem() As String, strItem_in_Times() As String
Dim i As Long, j As Long, n As Long
strSource = "02 05 06 12 14 28 04 06 12 30 31 32 02 08 13 28 29 30 01 02 05 16 20 26 01 07 08 12 16 21"
ReDim strItem_in_Times(0)
strItem = Split(strSource, " ")
For i = 1 To 33
strSource = Format(i, "0#")
n = 0
For j = 0 To UBound(strItem)
If strSource = strItem(j) Then n = n + 1
Next j
If n > UBound(strItem_in_Times) Then ReDim Preserve strItem_in_Times(n)
strItem_in_Times(n) = strItem_in_Times(n) & IIf(strItem_in_Times(n) = "", "", " ") & strSource
Next i
For i = 0 To UBound(strItem_in_Times)
Debug.Print "[" & Format(i, "0#") & "次] " & strItem_in_Times(i)
Next i
Dim strSource As String, strItem() As String, strItem_in_Times() As String
Dim i As Long, n As Long
strSource = "02 05 06 12 14 28 04 06 12 30 31 32 02 08 13 28 29 30 01 02 05 16 20 26 01 07 08 12 16 21"
List1.Clear
strItem = Split(strSource, " ")
For i = 0 To UBound(strItem)
n = SendMessagebyString(List1.hWND, LB_FINDSTRINGEXACT, -1, strItem(i))
If n = -1 Then '没有匹配列表项
List1.AddItem strItem(i)
List1.ItemData(List1.NewIndex) = 1
Else
List1.ItemData(n) = List1.ItemData(n) + 1
End If
Next i
strSource = "01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33"
strItem = Split(strSource, " ")
ReDim strItem_in_Times(0)
For i = 0 To UBound(strItem)
n = SendMessagebyString(List1.hWND, LB_FINDSTRINGEXACT, -1, strItem(i))
If n = -1 Then '没有匹配列表项
strItem_in_Times(0) = strItem_in_Times(0) & IIf(strItem_in_Times(0) = "", "", " ") & strItem(i)
Else
If List1.ItemData(n) > UBound(strItem_in_Times) Then ReDim Preserve strItem_in_Times(List1.ItemData(n))
strItem_in_Times(List1.ItemData(n)) = strItem_in_Times(List1.ItemData(n)) & IIf(strItem_in_Times(List1.ItemData(n)) = "", "", " ") & strItem(i)
End If
Next i
For i = 0 To UBound(strItem_in_Times)
Debug.Print "[" & Format(i, "0#") & "次] " & strItem_in_Times(i)
Next i
补充:VB , 基础类