a(1)=23
a(2)=25
a(3)=57
a(4)=55
.....
C=137如何从a数组中组出和为137个数随便.唉说不很清就是 从一堆数里碰出和为某一数的算法....
'------------------------------------------------------
别人给的算法...vb的Option ExplicitConst N = 6
Dim A(1 To N) As Integer
Dim B(1 To N) As Integer
Dim C As IntegerPrivate Sub Form_Load()
Dim I As Integer
A(1) = 23
A(2) = 25
A(3) = 57
A(4) = 55
A(5) = 10
A(6) = 20
For I = 1 To N
B(I) = 0
Next I
Debug.Print "Start"
C = 90
Try (1)
Debug.Print "Finish"
End
End SubPrivate Sub Try(ByVal M As Integer)
Dim I As Integer, J As Integer, K As Integer
For I = M To N
If B(I) = 0 And A(I) <= C Then
B(I) = 1
K = 0
For J = 1 To N
K = K + A(J) * B(J)
Next J
If K = C Then
For J = 1 To N
If B(J) = 1 Then Debug.Print A(J);
Next J
Debug.Print
End If
If M < N Then Try (I + 1)
B(I) = 0
End If
Next I
End Sub
'------------------------------------------------------
我在excel里的Dim N As Integer
Dim a(1 To 6000) As Double
Dim b(1 To 6000) As Double
Dim c As Double
Dim cc As String
Private Sub CommandButton1_Click()
N = ActiveSheet.UsedRange.Rows.Count - 1
Dim I As Integer
For I = 1 To N
If Application.WorksheetFunction.IsNumber(ActiveSheet.Cells(CStr(I), 4)) Then
a(I) = ActiveSheet.Cells(CStr(I), 4)
Else
a(I) = 0
End If
Next
For I = 1 To N
b(I) = 0
Next I
c = TextBox1.Value
Try (1)
End SubPrivate Sub Try(ByVal M As Integer)
Dim I As Integer, J As Integer, K As Double
For I = M To N
If b(I) = 0 And a(I) <= c Then
b(I) = 1
K = 0
For J = 1 To N
K = K + a(J) * b(J)
Next J
aaa = 0
aaa = CLng((K + 0.005) * 100) / 100
TextBox3.Value = aaa
DoEvents
If aaa = c Then
cc = ""
For J = 1 To N
If b(J) = 1 Then cc = cc + "+" + CStr(J)
Next J
TextBox2.Text = cc
End
End If
Try (I + 1)
b(I) = 0
End If
Next I
End Sub
'------------------------------------------------------------------------------
某朋友给了一个其它的算法.Function GetNum(m_a() As Integer, Sum As Integer) As String
For l = LBound(m_a()) To UBound(m_a())
For m = l To UBound(m_a())
If m_a(l) + m_a(m) = Sum Then
GetNum = GetNum & CStr(m_a(l)) & "+" & CStr(m_a(m)) & "=" & CStr(Sum) & ","
End If
Next m
Next l
End Function如果要是太多行的话速度太慢了.寻更好的算法.....
寻最佳算法.........................excel里面有上千行的数据.....
a(2)=25
a(3)=57
a(4)=55
.....
C=137如何从a数组中组出和为137个数随便.唉说不很清就是 从一堆数里碰出和为某一数的算法....
'------------------------------------------------------
别人给的算法...vb的Option ExplicitConst N = 6
Dim A(1 To N) As Integer
Dim B(1 To N) As Integer
Dim C As IntegerPrivate Sub Form_Load()
Dim I As Integer
A(1) = 23
A(2) = 25
A(3) = 57
A(4) = 55
A(5) = 10
A(6) = 20
For I = 1 To N
B(I) = 0
Next I
Debug.Print "Start"
C = 90
Try (1)
Debug.Print "Finish"
End
End SubPrivate Sub Try(ByVal M As Integer)
Dim I As Integer, J As Integer, K As Integer
For I = M To N
If B(I) = 0 And A(I) <= C Then
B(I) = 1
K = 0
For J = 1 To N
K = K + A(J) * B(J)
Next J
If K = C Then
For J = 1 To N
If B(J) = 1 Then Debug.Print A(J);
Next J
Debug.Print
End If
If M < N Then Try (I + 1)
B(I) = 0
End If
Next I
End Sub
'------------------------------------------------------
我在excel里的Dim N As Integer
Dim a(1 To 6000) As Double
Dim b(1 To 6000) As Double
Dim c As Double
Dim cc As String
Private Sub CommandButton1_Click()
N = ActiveSheet.UsedRange.Rows.Count - 1
Dim I As Integer
For I = 1 To N
If Application.WorksheetFunction.IsNumber(ActiveSheet.Cells(CStr(I), 4)) Then
a(I) = ActiveSheet.Cells(CStr(I), 4)
Else
a(I) = 0
End If
Next
For I = 1 To N
b(I) = 0
Next I
c = TextBox1.Value
Try (1)
End SubPrivate Sub Try(ByVal M As Integer)
Dim I As Integer, J As Integer, K As Double
For I = M To N
If b(I) = 0 And a(I) <= c Then
b(I) = 1
K = 0
For J = 1 To N
K = K + a(J) * b(J)
Next J
aaa = 0
aaa = CLng((K + 0.005) * 100) / 100
TextBox3.Value = aaa
DoEvents
If aaa = c Then
cc = ""
For J = 1 To N
If b(J) = 1 Then cc = cc + "+" + CStr(J)
Next J
TextBox2.Text = cc
End
End If
Try (I + 1)
b(I) = 0
End If
Next I
End Sub
'------------------------------------------------------------------------------
某朋友给了一个其它的算法.Function GetNum(m_a() As Integer, Sum As Integer) As String
For l = LBound(m_a()) To UBound(m_a())
For m = l To UBound(m_a())
If m_a(l) + m_a(m) = Sum Then
GetNum = GetNum & CStr(m_a(l)) & "+" & CStr(m_a(m)) & "=" & CStr(Sum) & ","
End If
Next m
Next l
End Function如果要是太多行的话速度太慢了.寻更好的算法.....
寻最佳算法.........................excel里面有上千行的数据.....
还是高手不乐意管呢
另建议B(I)使用布尔型,而不是整型。
另,这个问题含有计算机基础学科问题,现在的新人都不知道认真看基础了。
另建议B(I)使用布尔型,而不是整型。是因为VB里面 真为-1 假为0 所以他用0和1不要恶心!!!!出程序出结果才是最真的
VB、VBA的效率本身就不是很好,而浮点数运算更慢没有办法。小数点后保留五位。最终结果还得四舍五入。长整型可以吗????我只知道.net的长整是可以的.
另,这个问题含有计算机基础学科问题,现在的新人都不知道认真看基础了不是新人不新人的问题。毕竟我从96年就开始玩程序算法了。只是一时想不到什么好的思路。只所以在delphi里贴出还以为搞delphi的相对来说还有点高手。我在4个版里提出相同的问题。其它版连敢回复一句的都没有!!!!
叫你用布尔,不是叫你用布尔变量去做乘法运算,而是根据布尔值去决定是否累加,加法比乘法效率要高很多。
如果你的数据只需要4位小数,那么可以选择货币类型,这种类型是用整型为核心的定点小数,外观象小数,实际是整数,运算速度比小数快N倍。.NET的运行效率真不敢恭维,不过开发效率还算凑合吧,前提是有足够好的计算机。PS:我是学VB出身的,也用过C#,所有开发语言和环境都是自己摸索的。而且都作过项目,所以我自认还是有发言权去评论的,每种开发环境都有自己的优缺点。
而且高效算法为了防止字符串加减一次次分配释放内存,可能会用到指针地址操作,一次性分配大块内存等等。VBA也做不了。