【问题标题】:all possible combinations所有可能的组合
【发布时间】:2013-03-22 13:45:48
【问题描述】:

我需要获取所有可能组合的列表,而不是排列。

为了确保我有正确的名字,123 和 321 对我来说是同一个东西,应该只列出一次。

下面的代码可以满足我的需要,但我无法将其转换为 MS Access vba。

对不起,我知道这是基本的,并且已经被问了一百万次,但我找不到任何适合我的 MS Access。

 Sub test_print_nCr()
      print_nCr 7, 3, Range("A1")
    End Sub

2.

Public Function print_nCr(n As Integer, r As Integer, p As Range)

  c = 1
  internal_print_nCr n, r, p, 1, 1
End Function

3.

Public Function internal_print_nCr(n As Integer, r As Integer, ByVal p As Range, Optional i As Integer, Optional l As Integer) As Integer

  ' n is the number of items we are choosing from
  ' r is the number of items to choose
  ' p is the upper corner of the output range
  ' i is the minimum item we are allowed to pick
  ' l is how many levels we are in to the choosing
  ' c is the complete set we are working on

  If n < 1 Or r > n Or r < 0 Then Err.Raise 1
  If i < 1 Then i = 1
  If l < 1 Then l = 1
  If c < 1 Then c = 1
  If r = 0 Then
    p = 1
    Exit Function
  End If

  Dim x As Integer
  Dim y As Integer

  For x = i To n - r + 1
    If r = 1 Then
      If c > 1 Then
        For y = 0 To l - 2
          If p.Offset(c - 1, y) = "" Then p.Offset(c - 1, y) = p.Offset(c - 2, y)
        Next
      End If
      p.Offset(c - 1, l - 1) = x
      c = c + 1
    Else
      p.Offset(c - 1, l - 1) = x
      internal_print_nCr n, r - 1, p, x + 1, l + 1
    End If
  Next

End Function

再次感谢您

【问题讨论】:

  • 怎么能不转换呢?这看起来像 VBA 代码 - 你有错误吗?如果是这样,错误文本是什么,抛出错误的行是什么?如果没有错误,当您在调试器中单步执行时会发生什么?换句话说,什么是意外行为?
  • Excel 工作簿中的代码并使用单元格存储数据。对于 Access 功能来说,这不是一个很好的起点。
  • 是的,我应该提到,对不起,这是 excel 代码,它会复制工作簿中的值。我只需要能够将值传递给变量。
  • 只是想知道为什么这个话题被否决了,不管是谁做的,你能解释一下吗?或者我在哪里可以看到原因?
  • 您正在使用 MS Access,但您的问题中没有提到任何表格。如果要处理的数据在表中,您可以使用 CROSS JOIN 生成所有可能的组合,并排除您不需要的组合。

标签: ms-access vba


【解决方案1】:

我不确定这是否是最好的方法,但我会使用一种二进制表示。例如,考虑字母数 n=3 的单词“boy”。这个词有三个字母,所以你可以这样使用:

001 = y, 010 = o, 011 = 哦, 100 = b, 101 = 由, 110 = 博, 111 = 男孩。

左侧可以通过从 i=1 到 power(2,n)-1 的循环并将 i 转换为二进制数来完成。因此,您唯一需要做的就是使用非空位置来构建您的组合。

在 Knuth 中可能还有比这更有趣的东西。

【讨论】:

  • 这超出了我的范围,写这篇文章,这就是为什么我正在寻找我可以调整的现成代码。对不起,我什至不能说这是否是正确的答案,因为我不明白。不过谢谢你
【解决方案2】:

我在这里找到了这段代码,它给了我我需要的东西。您只需要创建一个包含 1-100 数字的表格。以下链接中的说明

enter link description here

谢谢大家

Public Sub buildquery(strN As String, K As Integer)
    Dim qd As DAO.QueryDef
    Dim intI As Integer
    Dim strsql As String
    Dim strSelect As String
    Dim strFrom As String
    Dim strWhere As String

    Set qd = CurrentDb.QueryDefs("QN")
    qd.sql = "SELECT N FROM tblN WHERE N IN (" & strN & ")"
    Set qd = Nothing
    strSelect = "SELECT QN.N "
    strFrom = "FROM QN "
    strWhere = "WHERE QN_1.N > QN.N "
    For intI = 1 To K - 1
        strSelect = strSelect & ", QN_" & intI & ".N AS N" & intI & " "
        strFrom = strFrom & ", QN AS QN_" & intI & " "
        If intI < K - 1 Then
            strWhere = strWhere & " AND QN_" & intI + 1 & ".N > QN_" & intI & ".N "
        End If
    Next
    strsql = strSelect & " INTO tblCombinations " & strFrom & strWhere
    DoCmd.SetWarnings False
    DoCmd.RunSQL strsql
    DoCmd.SetWarnings True
End Sub

然后测试

Public Sub testbuildquery()
  buildquery "1,2,3,4,5,6,7", 3
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多