当前位置:编程学习 > VB >>

利用vb语言把一组数字分组

比如一组数字[1,2,3,4,5,4,4,4,2,2,1,1,6],分为[1,1,1],[3],[2,2,2],[4,4,4,4],[5],[6]应该怎么分  vb 给你个思路吧
第一步,排序,至于用什么方法你自己定,什么冒泡的什么二分的办法太多,不介绍了。
第二步,做一个循环,当前这一个与上一个是相同的就放在同一个组中,不同的另起一组。 窗体上加一个 ListBox,Sorted 属性设置为 True。
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

Private Sub Command1_Click()
Dim strSource As String, strItem() As String, i As Long, n As Long

strSource = "[1,2,3,4,5,4,4,4,2,2,1,1,6]"

strSource = Replace(Replace(Replace(strSource, "[", ""), "]", ""), " ", "")
strItem = Split(strSource, ",")

List1.Clear
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

ReDim strItem(List1.ListCount - 1)
For i = 0 To List1.ListCount - 1
    strItem(i) = "["
    For n = 1 To List1.ItemData(i) - 1
        strItem(i) = strItem(i) & List1.List(i) & ","
    Next n
    strItem(i) = strItem(i) & List1.List(i) & "]"
    Debug.Print strItem(i)
Next i
Debug.Print Join(strItem, ",")
End Sub

结果:
[1,1,1]
[2,2,2]
[3]
[4,4,4,4]
[5]
[6]
[1,1,1],[2,2,2],[3],[4,4,4,4],[5],[6]
Option Explicit

' 工程引用: Microsoft Scripting Runtime

Private Type MembList
   BuffSize    As Long
   MembNum     As Long
   DataBuff()  As Long
End Type

Private Type GroupInfo
   ListSize    As Long
   GroupNum    As Long
   GroupList() As MembList
End Type

 ' *** 按“下标从0开始”处理数组 ***
Private stcDataGroup As GroupInfo      ' 分组信息管理变量

' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * *    将一组数据进行分组
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Private Sub Grouping(DataList() As Long)
   Dim objDict As Dictionary
   Dim p As Long, n As Long, v As Long
   Dim i As Long
   ' 初始化分组信息
   stcDataGroup.ListSize = 16
   stcDataGroup.GroupNum = 0
   ReDim stcDataGroup.GroupList(stcDataGroup.ListSize - 1)
   ' 开始处理数据
   Set objDict = New Dictionary
   For i = 0 To UBound(DataList)
      v = DataList(i)
      If (objDict.Exists(v)) Then
         p = objDict.Item(v)
         With stcDataGroup.GroupList(p)
            p = .MembNum
            If (p = .BuffSize) Then
               n = p + 4      ' 扩充列表长度。如果列表长度多数较大,宜取稍大的值。
               ReDim Preserve .DataBuff(n - 1)
               .BuffSize = n
            End If
            .DataBuff(p) = v
            .MembNum = p + 1  '
         End With
      Else
         p = stcDataGroup.GroupNum
         objDict.Add v, p
         'n = stcdatagroup.GroupList(p).MembNum
         If (p = stcDataGroup.ListSize) Then       '增加组数
            n = p + 8
            ' 每次扩充8个,可按你的需要改。组数多,宜取稍大的值。
            ' 每次扩充数大些,运行效率高点。
            stcDataGroup.ListSize = n
            ReDim Preserve stcDataGroup.GroupList(n - 1)
         End If
         ' 初始化新分组
         With stcDataGroup.GroupList(p)
            n = 16      '设定每组初始大小。分组长度大,就宜取稍大的值。
            .BuffSize = n
            ReDim .DataBuff(n - 1)
            .MembNum = 1
            .DataBuff(0) = v
         End With
         stcDataGroup.GroupNum = p + 1
      End If
   Next
   objDict.RemoveAll
End Sub

' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * *    将分组数据输出示例(分组信息调取示例)
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Private Sub ListGroup()
   Dim i&, j&
   
   For i = 0 To stcDataGroup.GroupNum - 1
      Debug.Print "第 " & i + 1 & " 组数据:"
      For j = 0 To stcDataGroup.GroupList(i).MembNum - 1
         Debug.Print stcDataGroup.GroupList(i).DataBuff(j);
      Next
      Debug.Print
   Next
End Sub

Private Sub Command1_Click()
   Dim aData() As Long
   Dim i&, sTxtBuf$()
   
   sTxtBuf = Split("1,2,3,4,5,4,4,4,2,2,1,1,6", ",")
   ReDim aData(UBound(sTxtBuf))
   For i = 0 To UBound(sTxtBuf)
      aData(i) = sTxtBuf(i)
   Next
   Call Grouping(aData)    ' 数据分组
   Call ListGroup          ' 输出结果示例
End Sub
牛人真多 哈哈  都是技术大牛 刚才突然想到一个问题,就是:“每个分组内,成员都是一样的”。
因此,数据类型 MembList 没必要用数组把各个成员写入,只要记录值是什么、有多少个就行了。
  这样的方案,还可以简化数据管理,并提高运行效率。

因此我的代码可以简化一下:
Option Explicit

' 工程引用: Microsoft Scripting Runtime

Private Type MembList
   MembNum     As Long
   Value       As Long
End Type

Private Type GroupInfo
   ListSize    As Long
   GroupNum    As Long
   GroupList() As MembList
End Type

 ' *** 按“下标从0开始”处理数组 ***
Private stcDataGroup As GroupInfo      ' 分组信息管理变量

' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * *    将一组数据进行分组
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Private Sub Grouping(DataList() As Long)
   Dim objDict As Dictionary
   Dim p As Long, n As Long, v As Long
   Dim i As Long
   ' 初始化分组信息
   stcDataGroup.ListSize = 16
   stcDataGroup.GroupNum = 0
   ReDim stcDataGroup.GroupList(stcDataGroup.ListSize - 1)
   ' 开始处理数据
   Set objDict = New Dictionary
   For i = 0 To UBound(DataList)
      v = DataList(i)
      If (objDict.Exists(v)) Then
         With stcDataGroup.GroupList(objDict.Item(v))
            .MembNum = .MembNum + 1
         End With
      Else
         p = stcDataGroup.GroupNum
         objDict.Add v, p
         If (p = stcDataGroup.ListSize) Then       '增加组数
            n = p + 8
            ' 每次扩充8个,可按你的需要改。组数多,宜取稍大的值。
            ' 每次扩充数大些,运行效率高点。
            stcDataGroup.ListSize = n
            ReDim Preserve stcDataGroup.GroupList(n - 1)
         End If
         ' 新分组数据记录
         stcDataGroup.GroupList(p).MembNum = 1
         stcDataGroup.GroupList(p).Value = v
         stcDataGroup.GroupNum = p + 1
      End If
   Next
   objDict.RemoveAll
End Sub

' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * *    将分组数据输出示例(分组信息调取示例)
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Private Sub ListGroup()
   Dim i&, j&
   For i = 0 To stcDataGroup.GroupNum - 1
      Debug.Print "第 " & i + 1 & " 组数据:"
      For j = 1 To stcDataGroup.GroupList(i).MembNum
         Debug.Print stcDataGroup.GroupList(i).Value;
      Next
      Debug.Print
   Next
End Sub

Private Sub Command1_Click()
   Dim aData() As Long
   Dim i&, sTxtBuf$()
   
   sTxtBuf = Split("1,2,3,4,5,4,4,4,2,2,1,1,6", ",")
   ReDim aData(UBound(sTxtBuf))
   For i = 0 To UBound(sTxtBuf)
      aData(i) = sTxtBuf(i)
   Next
   Call Grouping(aData)    ' 数据分组
   Call ListGroup          ' 输出结果示例
End Sub
Private Sub Grouping(DataList() As Long) 中, n As Long, 可以不要了。

后面放大分组数那儿这样改下:
If (p = stcDataGroup.ListSize) Then       '增加组数
   stcDataGroup.ListSize = p + 8
   ReDim Preserve stcDataGroup.GroupList(p + 7)
End If

VB.NET只要1行:
Dim Result = "1,2,3,4,5,4,4,4,2,2,1,1,6".Split(",").GroupBy(Function(x) x)
引用 楼主 playboyhdy 的回复:
比如一组数字[1,2,3,4,5,4,4,4,2,2,1,1,6],分为[1,1,1],[3],[2,2,2],[4,4,4,4],[5],[6]应该怎么分 


怎么没人问,为什么分好后,3要排到达前面?
引用 8 楼 bcrun 的回复:
Quote: 引用 楼主 playboyhdy 的回复:

比如一组数字[1,2,3,4,5,4,4,4,2,2,1,1,6],分为[1,1,1],[3],[2,2,2],[4,4,4,4],[5],[6]应该怎么分 


怎么没人问,为什么分好后,3要排到达前面?

因为原始数据3在前面。这不需要特别问。标准的算法就应该这样输出。
引用 8 楼 bcrun 的回复:
Quote: 引用 楼主 playboyhdy 的回复:

比如一组数字[1,2,3,4,5,4,4,4,2,2,1,1,6],分为[1,1,1],[3],[2,2,2],[4,4,4,4],[5],[6]应该怎么分 


怎么没人问,为什么分好后,3要排到达前面?

看错了,标准的算法应该输出 1 2 3 4 5 6这样几个分组。
引用 8 楼 bcrun 的回复:
Quote: 引用 楼主 playboyhdy 的回复:

比如一组数字[1,2,3,4,5,4,4,4,2,2,1,1,6],分为[1,1,1],[3],[2,2,2],[4,4,4,4],[5],[6]应该怎么分 


怎么没人问,为什么分好后,3要排到达前面?

你想得真多啊。

我倒是真没注意这个。
应该是楼主的失误吧,要不然,感觉无法解释这个原因了。
引用 8 楼 bcrun 的回复:
Quote: 引用 楼主 playboyhdy 的回复:

比如一组数字[1,2,3,4,5,4,4,4,2,2,1,1,6],分为[1,1,1],[3],[2,2,2],[4,4,4,4],[5],[6]应该怎么分 


怎么没人问,为什么分好后,3要排到达前面?


想过,但是觉得没有根据,也没有必要。 dim S as string 
dim U
dim I as long 
dim U09(9) as string
S=[1,2,3,4,5,4,4,4,2,2,1,1,6]
S=right(S,len(S)-1)
S=left(S,len(S)-1) '去掉前后[]

U=split(S,",") '根据 , 分成 数组 U

for i=0 to LBound(U)
  U09(cint(U(i))=U09(cint(U(i)) & iif(U09(cint(U(i))="","",",") & cstr(U(i))
nexi i

S="["
for i = 0 to 9 
  if U09(i)<>"" then S= iif(s="[","",",") & U09(i) & "]"
next i

msgbox S S="["
for i = 0 to 9 
  if U09(i)<>"" then S= iif(s="[","",",") & S & U09(i) & "]"
next i

#13 最后忘了个 S 
 if U09(i)<>"" then S= iif(s="[","",",[") & S & U09(i) & "]" 牛人真多...... 代码真的好多。勉强看得明白吧。。。
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,