日期:2014-05-17 浏览次数:20859 次
Function CreateResult(a,b,m) Dim result result="" n=UBound(a)-1 tempTest ="" j=0 For i=0 To n tempTest = tempTest & "," & b(i) j=j+1 If b(i)=1 Then result = result & "+" & a(i) End If Next CreateResult = result End Function Function Combination(a,m) '从数组a中选取m个数进行组合 Dim n,list Dim b() n = UBound(a)-1 ReDim b(n) '辅助数组。首先初始化,将数组前m个元素置为1,表示第一个组合为m个数 For i=0 To n If i<m Then b(i)=1 Else b(i)=0 End If Next list = list & "|" & CreateResult(a,b,m) tempEnd = "N" Do While tempEnd ="N" findFirst = "N" swap = "N" For i=0 To n L=0 'Call JS_AlertBackEnd(i) If findFirst="N" And b(i)=1 Then k=i findFirst = "Y" End If If b(i)=1 And b(i+1)=0 Then b(i)=0 b(i+1)=1 swap = "Y" If k<>1 Then For L=0 To i-k b(L)=b(k+1) Next For L=i-k To i b(L)=0 Next End If If k=i And i=n-m Then tempEnd = "Y" End If End If If swap="Y" Then Exit For End If Next list = list & "|" & CreateResult(a,b,m) Loop Combination = list End Function
If (i+1)<=n And b(i)=1 And b(i+1)=0 Then '多加一个是否越界的判断
------解决方案--------------------
ReDim b(n) <-- b的下标是0 到n
For i=0 To n <-- 这里 i 是循环到n的, i=n时 i+1必然超过界限
...
If b(i)=1 And b(i+1)=0 Then <----这里 i=n时 就发生了i+1越界
------解决方案--------------------
下标是从0开始的,你加一就多了啊
------解决方案--------------------
你加一下判断就行了,3楼的说的很对
------解决方案--------------------
For i=0 To n
说明i是从0开始的
If b(i)=1 And b(i+1)=0 Then
这里的b(i+1)=0
这里怎么能=0呢,最小也结果也是1啊