求一个小程序的代码:有1到20这样20个数,任意组合其中的数得到19这个数

yelang771 2004-07-24 12:19:18

就是可以2个数字组合,也可以3个数字组合,4个,5个========
1个当然也要了,要能得到所有的情况。。。。。
...全文
471 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
daviddivad 2004-07-26
  • 打赏
  • 举报
回复
上面那个 : '数据的无序组合(非排列)
是几个数字相+的情况吧?看不太懂

下面这个:
Set mRet = fun_Combination(mCol, 7)
If mRet.Count > 0 Then
For intI = 1 To mRet.Count
Debug.Print mRet.Item(intI)
Next intI
End If
Set mCol = Nothing
Set mRet = Nothing
为什么要这样写? 不象我问题的答案, 好象我太菜了啊

////////////////////////////////////
这只是生成一个7个数的无序组合
你可以再用个循环分别生产1至20个数的无序组合
然后去判断每个组合的和是否为19即可
cqm2099 2004-07-25
  • 打赏
  • 举报
回复
学QB时做过这道题,算法跟楼上差不多,实现不是很难!~
Mars.CN 2004-07-25
  • 打赏
  • 举报
回复
哦哦哦……
有这么复杂吗????
Dim Sz(1 to 20) As Integer
Dim Zh As Integer , I As Integer
Dim Ssz(1 to 20) As Integer
For i=1 to 19
do While Zh<19
i=i+1
Zh=Zh+Sz(i)
Ssz(i)=Sz(i)
If Zh=19 Then
For f=1 to 20
if ssz(f) <>0 Then
Print ssz(f)
End if
Loop
For f=1 to 20
Ssz(f)=0
Next f
Next i
yelang771 2004-07-24
  • 打赏
  • 举报
回复
我要最简单的几个数字相+的就行

有1 2 3 4 。。。。。。。。。20 相加减乘除模方得 19 就更好了
liul17 2004-07-24
  • 打赏
  • 举报
回复
1 2 3 4 。。。。。。。。。20 相加减乘除模方得 19?
yelang771 2004-07-24
  • 打赏
  • 举报
回复
排列组合就行

有 类似算24点那样 给我参考参考 就更好了-_-
daviddivad 2004-07-24
  • 打赏
  • 举报
回复
什么意思?
你是要写个排列组合的程序,还是类似算24点那样,用+-*/的算法算出结果为19的组合?
yelang771 2004-07-24
  • 打赏
  • 举报
回复
哇赛!xiexiedajia
liuyan4794 2004-07-24
  • 打赏
  • 举报
回复
与楼上的方法不一致,结果也是54种,不知道那种方法的速度快
Dim data(19) As Integer
Dim NUM As Integer
Private Sub Command1_Click()
data(1) = 1
NUM = 0
cacu 1
Debug.Print NUM
End Sub

Private Sub cacu(count As Integer)
Dim I As Integer
Dim cn As Integer
Dim st As String
cn = 0
For I = 1 To count
cn = cn + data(I)
Next I
If cn > 19 Then
count = count - 1
data(count) = data(count) + 1
cacu count
ElseIf cn < 19 Then
count = count + 1
data(count) = data(count - 1) + 1
cacu count
ElseIf cn = 19 Then
st = Str(data(1))
For I = 2 To count
st = st + "+" + Str(data(I))
Next I
st = st + "=19"
NUM = NUM + 1
Debug.Print st
count = count - 1
If data(count) < 19 And count > 0 Then
data(count) = data(count) + 1
cacu count
End If
End If
End Sub
risingo 2004-07-24
  • 打赏
  • 举报
回复
好象结果很多啊!
  运算得过来吗?
northwolves 2004-07-24
  • 打赏
  • 举报
回复
上面程序返回:
1+3+4+5+6=19
1+2+4+5+7=19
3+4+5+7=19
1+2+3+6+7=19
2+4+6+7=19
1+5+6+7=19
1+2+3+5+8=19
2+4+5+8=19
2+3+6+8=19
1+4+6+8=19
5+6+8=19
1+3+7+8=19
4+7+8=19
1+2+3+4+9=19
2+3+5+9=19
1+4+5+9=19
1+3+6+9=19
4+6+9=19
1+2+7+9=19
3+7+9=19
2+8+9=19
2+3+4+10=19
1+3+5+10=19
4+5+10=19
1+2+6+10=19
3+6+10=19
2+7+10=19
1+8+10=19
9+10=19
1+3+4+11=19
1+2+5+11=19
3+5+11=19
2+6+11=19
1+7+11=19
8+11=19
1+2+4+12=19
3+4+12=19
2+5+12=19
1+6+12=19
7+12=19
1+2+3+13=19
2+4+13=19
1+5+13=19
6+13=19
2+3+14=19
1+4+14=19
5+14=19
1+3+15=19
4+15=19
1+2+16=19
3+16=19
2+17=19
1+18=19
19=19
54 种方法
northwolves 2004-07-24
  • 打赏
  • 举报
回复
20个数字中,20可弃去,其余19个数字可用2^18的二进制中各自位置是0或1决定取舍:


Sub GETALL(ByVal SUM As Integer, ByRef RESULT() As String)
Dim A(), I As Long, NUM As Integer, ALLNUM As Long
Dim TEMP1 As Long, TEMP2(1 To 20) As Integer, TEMP As Integer

ALLNUM = 0
For I = 1 To 2 ^ 18
TEMP1 = I
NUM = 0
For j = 1 To 19 '转换为二进制
TEMP2(j) = TEMP1 And 1 '0 or 1
TEMP1 = TEMP1 \ 2
If TEMP2(j) = 1 Then
NUM = NUM + 1
ReDim Preserve A(1 To NUM)
A(NUM) = j
End If
Next


TEMP = 0
For j = 1 To NUM
TEMP = TEMP + A(j)
Next

If TEMP = SUM Then '满足条件

ALLNUM = ALLNUM + 1
ReDim Preserve RESULT(1 To ALLNUM)

RESULT(ALLNUM) = Join(A, "+") & "=" & SUM '结果保存
Debug.Print RESULT(ALLNUM)
End If
Next
Debug.Print ALLNUM &" 种方法"
MsgBox "OK"
End Sub

Private Sub Command1_Click()
Dim S() As String
GETALL 19, S '和为19 时
End Sub
yelang771 2004-07-24
  • 打赏
  • 举报
回复
我去看会书,想想,4点多过来看看,谢谢~
yelang771 2004-07-24
  • 打赏
  • 举报
回复
bobbyxby(i386)的 这是任意数字相+的情况吧?


这是2个数字相+得到19的情况:
Private Sub Form_Load()
Dim i As Integer, j As Integer, s As Integer
For i = 0 To 20
For j = i + 1 To 20
s = i + j
If s = 19 Then
List1.AddItem i & "+" & j & "=19"
End If
Next
Next
按这样写下去,是递规模拟了吧。。。。

谢谢大家~~~~~~
yelang771 2004-07-24
  • 打赏
  • 举报
回复
上面那个 : '数据的无序组合(非排列)
是几个数字相+的情况吧?看不太懂

下面这个:
Set mRet = fun_Combination(mCol, 7)
If mRet.Count > 0 Then
For intI = 1 To mRet.Count
Debug.Print mRet.Item(intI)
Next intI
End If
Set mCol = Nothing
Set mRet = Nothing
为什么要这样写? 不象我问题的答案, 好象我太菜了啊
xuboying 2004-07-24
  • 打赏
  • 举报
回复
好久不写算法了,好辛苦。。。。
在一个类模块Class1.cls:
Option Explicit
Dim a(0 To 30) As Integer
Dim Point As Integer
Public Sub push(num)
Point = Point + 1
a(Point) = num
End Sub

Private Sub Class_Initialize()
Point = -1
End Sub
Public Function pop()
pop = a(Point)
Point = Point - 1
End Function

Public Function sum()
Dim i
For i = 0 To Point
sum = sum + a(i)
Next
End Function

Public Function Button()
Button = a(0)
End Function
Public Function top()
top = a(Point)
End Function

Public Function Getnum(i As Integer)
Getnum = a(i)
End Function

Public Function num()
num = Point
End Function

在窗体里面Form1.frm:
添加一个按钮和一个多行文本框
Option Explicit
Dim stack As New Class1
Dim currentNum As Integer

Private Sub Command1_Click()
Dim i As Integer
currentNum = 0
Do
Select Case stack.sum
Case Is < 19
currentNum = currentNum + 1
stack.push (currentNum)
Case 19
For i = 0 To stack.num
Text1.Text = Text1.Text & stack.Getnum(i) & " "
Next
Text1.Text = Text1.Text & vbCrLf
Do
stack.pop
Loop Until stack.sum < 19
currentNum = stack.top + 1
stack.pop
stack.push (currentNum)
Case Is > 19
Do
stack.pop
Loop Until stack.sum < 19
currentNum = stack.top + 1
stack.pop
stack.push (currentNum)
End Select
DoEvents
Loop Until stack.Button = 19
Text1.Text = Text1.Text & "19"
End Sub

类模块实际上就是一个堆栈,本来想用递规模拟的,但是太麻烦了
daviddivad 2004-07-24
  • 打赏
  • 举报
回复
'mData 组合前的原始数据,其类型为Collection。
'mN 组合数据的单组数目
'mSplit 可选的,用于返回的组合数据两两之间的分隔符,默认为TAB。类型为String。
'mPos 不用的参数,它只用于函数本身的递归调用。

Private Sub Command1_Click()
Dim mCol As New Collection
Dim mRet As Collection
Dim intI As Integer
For intI = 1 To 20
mCol.Add CStr(intI), CStr(intI)
Next intI
Set mRet = fun_Combination(mCol, 7)
If mRet.Count > 0 Then
For intI = 1 To mRet.Count
Debug.Print mRet.Item(intI)
Next intI
End If
Set mCol = Nothing
Set mRet = Nothing
End Sub
daviddivad 2004-07-24
  • 打赏
  • 举报
回复
'数据的无序组合(非排列)
Public Function fun_Combination(ByVal mData As Collection, ByVal mN As Long, _
Optional ByVal mSplit As String = vbTab, Optional ByVal mPos As Long = 0) As Collection
Dim intI As Integer
Dim intJ As Integer
Dim mDataTemp As New Collection
Dim mSubData As Collection

If mN = 1 Then
For intI = mPos + 1 To mData.Count
mDataTemp.Add mData.Item(intI)
Next intI
GoTo FunctionEnd
End If
For intI = mPos + 1 To mData.Count
Set mSubData = fun_Combination(mData, mN - 1, mSplit, intI)
For intJ = 1 To mSubData.Count
mDataTemp.Add mData.Item(intI) & mSplit & mSubData(intJ)
Next intJ
Next intI
FunctionEnd:
Set fun_Combination = mDataTemp
End Function

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧