|
同时假定从中取4个元素数,那么有:
① 假设以0-9这10个数字作为对象元素,则Serial 为自然数序列,如 0000-9999
具体为:0000,0001,0002,……9998,9999
② 假设以1-8这8个数字作为对象元素,Combin 为组合结果,如 1234-5678
具体为:1234,1235,1236,……4678,5678
③ 假设以1-4这4个数字作为对象元素,Permut 为排列结果,如 1234-4321
具体为:1234,1243,1324,……4312,4321
那么上述3种不同要求序列的通用代码如下:
自定义函数 Arr_SerialCombinPermut(m,n,[z],[l]) 得到一个n列的数组。
其中,参数m为终点数值,n为取数位数
z为序列类型,默认z=0时返回序列,z=1时返回组合结果,z=2时返回排列结果。
l为起点数值(自然序列时默认 l=0,也可自行设定,其它组合或排列时不需要该参数)- Function Arr_SerialCombinPermut(m&, n&, Optional z& = 0, Optional l& = 0)
- Dim i&, j&, k&, t&
- If z = 0 Then k = (m - l + 1) ^ n
- If z = 1 Then k = WorksheetFunction.Combin(m, n): l = 1
- If z = 2 Then k = WorksheetFunction.Permut(m, n): ReDim c&(1 To m)
-
- ReDim a&(1 To n), b&(k - 1, 1 To n)
- For j = 1 To n
- If z = 0 Then a(j) = l
- If z = 1 Then a(j) = j
- If z = 2 Then a(j) = j: c(j) = 1
- b(0, j) = a(j) 'ar(a(j))
- Next
-
- For i = 1 To k - 1
- For j = n To 1 Step -1
- If z = 0 Then
- If a(j) < m Then a(j) = a(j) + 1: Exit For
- ElseIf z = 1 Then
- If a(j) < m - n + j Then a(j) = a(j) + 1: Exit For
- Else 'If z = 2 Then
- c(a(j)) = 0
- For t = a(j) + 1 To m
- If c(t) = 0 Then Exit For
- Next
- If t <= m Then c(t) = 1: a(j) = t: Exit For
- End If
- Next
- For j = j + 1 To n
- If z = 0 Then
- a(j) = l
- ElseIf z = 1 Then
- a(j) = a(j - 1) + 1
- Else 'If z = 2 Then
- For t = 1 To m
- If c(t) = 0 Then Exit For
- Next
- c(t) = 1: a(j) = t
- End If
- Next
- For j = 1 To n
- b(i, j) = a(j) 'ar(a(j))
- Next
- Next
- Arr_SerialCombinPermut = b
- End Function
复制代码 其实本帖有代码就可以了,不需要附件。 |
评分
-
5
查看全部评分
-
|