7,764
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private mdctSpecial As New Dictionary
Private mdctCommon As New Dictionary
Private mcolSource As New Collection
Private Function GetRandomNumber(ByVal intBegin As Integer, ByVal intEnd As Integer) As Integer
Randomize
GetRandomNumber = Int((intEnd - intBegin + 1) * Rnd + intBegin)
End Function
Private Function PickFromSource() As Integer
Dim intIndex As Integer
intIndex = GetRandomNumber(1, mcolSource.Count)
PickFromSource = mcolSource.Item(intIndex)
mcolSource.Remove intIndex
End Function
Private Sub GenerateSourceData()
Dim intCount As Integer
intCount = GetRandomNumber(3, 100)
Dim i As Integer
For i = 1 To intCount
mcolSource.Add i
Next
End Sub
Private Sub DevideIntoGroups()
Set mdctSpecial.Item(1) = New Collection
Set mdctSpecial.Item(2) = New Collection
Set mdctSpecial.Item(3) = New Collection
Select Case mcolSource.Count
Case 3
mdctSpecial(1).Add PickFromSource
mdctSpecial(2).Add PickFromSource
mdctSpecial(3).Add PickFromSource
Case 4
mdctSpecial(1).Add PickFromSource
mdctSpecial(1).Add PickFromSource
mdctSpecial(2).Add PickFromSource
mdctSpecial(3).Add PickFromSource
Case Else
If mcolSource.Count Mod 2 = 1 Then
mdctSpecial(1).Add PickFromSource
mdctSpecial(1).Add PickFromSource
mdctSpecial(2).Add PickFromSource
mdctSpecial(2).Add PickFromSource
mdctSpecial(3).Add PickFromSource
Else
mdctSpecial(1).Add PickFromSource
mdctSpecial(1).Add PickFromSource
mdctSpecial(2).Add PickFromSource
mdctSpecial(2).Add PickFromSource
mdctSpecial(3).Add PickFromSource
mdctSpecial(3).Add PickFromSource
End If
End Select
Debug.Assert mcolSource.Count Mod 2 = 0
Dim i As Integer
For i = 1 To mcolSource.Count \ 2
Set mdctCommon.Item(i) = New Collection
mdctCommon(i).Add PickFromSource
mdctCommon(i).Add PickFromSource
Next
End Sub
Private Sub Form_Load()
GenerateSourceData
Debug.Print mcolSource.Count
DevideIntoGroups
Dim varKey As Variant
For Each varKey In mdctSpecial
Dim varItem As Variant
For Each varItem In mdctSpecial(varKey)
Debug.Print "特殊岗位" & varKey & ": " & "人员" & varItem
Next
Next
For Each varKey In mdctCommon
For Each varItem In mdctCommon(varKey)
Debug.Print "正常岗位" & varKey & ": " & "人员" & varItem
Next
Next
Unload Me
End Sub
Const workernum = 20
Dim aryTestName(1 To workernum) As String
Dim i As Integer
Dim strtmp As String
Dim idx1 As Integer, idx2 As Integer
'模拟人员塞入数组,这里因为例子少于26个人,所以用字母来区分人员名字
For i = 1 To workernum
aryTestName(i) = "人员" & Chr(64 + i)
Next
Randomize Now() '弄个随机种子
For i = 1 To 10000 '做10000次随机调序打乱顺序,随机调序的次数建议为数组元素数量的10倍以上
idx1 = Int(Rnd * (UBound(aryTestName) - LBound(aryTestName) + 1)) + LBound(aryTestName) '生成1个数组下标范围内随机整数
idx2 = Int(Rnd * (UBound(aryTestName) - LBound(aryTestName) + 1)) + LBound(aryTestName) '再生成1个数组下标范围内随机整数
'把两个下标对应的数组元素进行对换
strtmp = aryTestName(idx1)
aryTestName(idx1) = aryTestName(idx2)
aryTestName(idx2) = strtmp
Next
'输出
For i = LBound(aryTestName) To UBound(aryTestName)
Debug.Print "第" & Fix((i - 1) / 5) + 1 & "组 " & IIf(((i - 1) Mod 5) < 2, "正常岗位" & ((i - 1) Mod 5) + 1, "特殊岗位" & ((i - 1) Mod 5) - 1) & ":" & aryTestName(i)
Next
一组输出:
第1组 正常岗位1:人员G
第1组 正常岗位2:人员H
第1组 特殊岗位1:人员C
第1组 特殊岗位2:人员E
第1组 特殊岗位3:人员T
第2组 正常岗位1:人员I
第2组 正常岗位2:人员L
第2组 特殊岗位1:人员S
第2组 特殊岗位2:人员M
第2组 特殊岗位3:人员P
第3组 正常岗位1:人员O
第3组 正常岗位2:人员F
第3组 特殊岗位1:人员N
第3组 特殊岗位2:人员D
第3组 特殊岗位3:人员R
第4组 正常岗位1:人员B
第4组 正常岗位2:人员Q
第4组 特殊岗位1:人员K
第4组 特殊岗位2:人员A
第4组 特殊岗位3:人员J