生成所有排列,省略那些至少不是最大顺序的排列

时间:2017-07-17 17:39:59

标签: excel excel-vba vba

我有一组数字,1-33。我需要一种快速的方法来生成这些数字中的三个的每个排列,从而导致升序。

示例:
7 19 25
1 2 3
10 20 30

但不是:
7 5 9
11 23 22

有没有办法在Excel中执行此操作?

由于

3 个答案:

答案 0 :(得分:2)

这将生成从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 :(得分:1)

或许这样的事情?

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

答案 2 :(得分:0)

使用A1B1C1初始化123, 然后输入

=IF((B1=32)*(C1=33),A1+1,A1)
<{1>}中的

A2
<{1>}和

中的

=IF(A2=A1,IF(C1<33,B1,B1+1),A2+1)
B2

中的

enter image description here

然后将单元格=MAX(B2+1,MOD(C1,33)+1) 向下复制/拖动到最后一行,C2A2:C231

enter image description here