产生所有的排列,省略那些最不重要的顺序
我有一组数字1-33。 我需要一个快速的方法来产生这些数字中的三个的每个排列,导致从小到大的顺序。
例子:
7 19 25
1 2 3
10 20 30
但不是:
7 5 9
11 23 22
有没有办法在Excel中做到这一点?
谢谢
也许这样的事情?
Sub Testing123() Dim seedMax As Integer Dim a As Integer Dim b As Integer Dim c As Integer seedMax = 33 For a = 1 To seedMax For b = a + 1 To seedMax For c = b + 1 To seedMax Debug.Print a, b, c Next c Next b Next a End Sub
将其写入工作表:
Sub Testing123withSheetWrite() Dim a As Integer Dim b As Integer Dim c As Integer Dim seedMax As Integer: seedMax = 33 Dim x As Long: x = 1 Dim y As Long: y = 1 For a = 1 To seedMax For b = a + 1 To seedMax For c = b + 1 To seedMax Debug.Print a, b, c Cells(x, y + 0) = a Cells(x, y + 1) = b Cells(x, y + 2) = c x = x + 1 Next c Next b Next a End Sub
这将生成从1到33的所有5456个整数的组合
Sub ListUm() Dim i As Long, j As Long, k As Long, Z As Long Z = 1 For i = 1 To 31 For j = i + 1 To 32 For k = j + 1 To 33 Cells(Z, 1) = i & "," & j & "," & k Z = Z + 1 Next k Next j Next i End Sub
既然你有一个指定的顺序,你可以使用组合而不是排列 。
用1
初始化A1
, B1
, C1
,然后input
=IF((B1=32)*(C1=33),A1+1,A1)
在A2
,
=IF(A2=A1,IF(C1<33,B1,B1+1),A2+1)
在B2
和
=MAX(B2+1,MOD(C1,33)+1)
在C2
然后复制/拖动单元格A2:C2
直到最后一行, 33
: