设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1236|回复: 6
打印 上一主题 下一主题

[模块/函数] 请教如何将组合数字全部列出的问题!

[复制链接]
跳转到指定楼层
1#
发表于 2022-12-25 09:58:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1、2、3,二位组合有12、13、23
1、2、3、4、5,三位的组合有123、124、125、134、135、145、234、235、245、345等
请教!如何用代码完全正确的列出?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2022-12-25 17:22:39 | 只看该作者
循环
回复

使用道具 举报

点击这里给我发消息

3#
发表于 2022-12-26 10:20:46 | 只看该作者
多个循环嵌套
4#
 楼主| 发表于 2022-12-27 15:55:21 | 只看该作者
:(:o:(:o:(:o
5#
 楼主| 发表于 2023-2-19 17:53:57 | 只看该作者
这是ChatGPT给的代码:
Public Sub ListCombinations(n As Integer, m As Integer)
    Dim a() As Integer
    ReDim a(1 To m)
    Combination 1, n, a, 1, m
End Sub
Private Sub Combination(t As Integer, n As Integer, a() As Integer, k As Integer, m As Integer)
    Dim i As Integer
    If k > m Then
        Debug.Print Join(a, " ")
        Exit Sub
    End If
    For i = t To n
        a(k) = i
        Combination i + 1, n, a, k + 1, m
    Next i
End Sub
运行报错!
       Debug.Print Join(a, " ")
“无效的过程调用或参数!”
改MsgBox也不行,请高手帮忙看看如何修改可以吗?

6#
发表于 2023-2-20 04:51:22 | 只看该作者
sxgaobo 发表于 2023-2-19 17:53
这是ChatGPT给的代码:
Public Sub ListCombinations(n As Integer, m As Integer)
    Dim a() As Integ ...

修改如下:
  1. Public Sub ListCombinations(n As Integer, m As Integer)
  2.     Dim a() As Variant
  3.     ReDim a(1 To m)
  4.     Combination 1, n, a, 1, m
  5. End Sub
  6. Private Sub Combination(t As Integer, n As Integer, a() As Variant, k As Integer, m As Integer)
  7.     Dim i As Integer
  8.     If k > m Then
  9.         Debug.Print Join(a, " ")
  10.         Exit Sub
  11.     End If
  12.     For i = t To n
  13.         a(k) = i
  14.         Combination i + 1, n, a, k + 1, m
  15.     Next i
  16. End Sub


  17. Sub test()
  18.     ListCombinations 5, 3
  19. End Sub
复制代码
数组类型应该指定为变型,而不是整型。
7#
 楼主| 发表于 2023-2-20 09:51:10 | 只看该作者
roych 发表于 2023-2-20 04:51
修改如下:
数组类型应该指定为变型,而不是整型。

搞定!谢谢高手出手
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-27 04:56 , Processed in 0.117369 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表