下面这个:
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
为什么要这样写? 不象我问题的答案, 好象我太菜了啊
哦哦哦……
有这么复杂吗????
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
与楼上的方法不一致,结果也是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
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
这是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
按这样写下去,是递规模拟了吧。。。。
下面这个:
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
为什么要这样写? 不象我问题的答案, 好象我太菜了啊
好久不写算法了,好辛苦。。。。
在一个类模块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
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
'数据的无序组合(非排列)
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