求EXCEL的VBA代码,用“1、2、3、*”四个字符,生成14位排列组合,要求每个组合123都用到,*出现4次。 88
2个回答
展开全部
Sub 随机14位数()
Dim A(1 To 14), B(1 To 14) As String
Dim S As String
Dim i, j As Integer
Dim Dic As Object
'填充数组A,先填1、2、3,4个*,其余的随机填充1、2、3【保证1、2、3至少出现一次,保证只有4个*】
'随机打乱数组A
'判断结果是否包含4个连续一样的字符,如果存在,重新填充、重新打乱
ST:
For i = 1 To 14
If i < 4 Then
A(i) = i
ElseIf i < 8 Then
A(i) = "*"
Else
Randomize
A(i) = Int(Rnd() * 3 + 1)
End If
Next i
Set Dic = CreateObject("Scripting.Dictionary")
For k = 1 To 14
Do
Randomize
j = Int(Rnd() * 14) + 1
Loop While Dic.exists(j)
Dic.Add j, ""
B(k) = A(j)
Next k
S = Join(B, "")
If InStr(S, "1111") Or InStr(S, "2222") Or InStr(S, "3333") Or InStr(S, "****") Then GoTo ST
Debug.Print S
End Sub
展开全部
Private Sub Command1_Click()
Debug.Print GetNumStr
End Sub
'生成数字的函数
'引用正则表达式
'项目引用 Microsoft VBScript Regular Expressions 5.5
Function GetNumStr() As String
Dim allStr As String, temp1 As String, k As Integer
allStr = "123*123*123*123*123*"
Dim reg0 As New RegExp, reg1 As New RegExp, reg2 As New RegExp, reg3 As New RegExp
reg0.Pattern = "([1]{4,})|([2]{4,})|([3]{4,})|([\*]{4,})"
reg1.Pattern = "[1]+": reg2.Pattern = "[2]+": reg3.Pattern = "[3]+"
Dim i As Integer
Do
Dim tempStr As String
tempStr = ""
temp1 = allStr
For i = 0 To 13
Randomize
k = (Rnd * (Len(temp1) - 1)) + 1
Print k, temp1
tempStr = tempStr & Mid(temp1, k, 1)
temp1 = Left(temp1, k - 1) & Right(temp1, Len(temp1) - k)
Next
Loop While reg0.Test(tempStr) Or Not (reg1.Test(tempStr) And reg2.Test(tempStr) And reg3.Test(tempStr))
GetNumStr = tempStr
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |