新手求助
具体代码如下:Dim n(9),sum
Dim e1,e2,e3,e4,e5,e6
Dim oExcel,oWb,oSheet,oPathoPath = createobject("wscript.shell").currentdirectory
Set oExcel= CreateObject("Excel.Application") 
Set oWb = oExcel.Workbooks.Open(oPath&"\nums.xls") 
Set oSheet = oWb.Sheets("Sheet1")For i=0 To 9
    n(i) = i
Next sum = 10For e6=0 To 9
    For e5=0 To 9
        For e4=0 To 9
            For e3=0 To 9
                For e2=0 To 9
                    For e1=0 To 9
                        If n(e5,1)+n(e6,1)+n(e7,1)+n(e8,1)+n(e9,1)+n(e10,1) = sum And n(e1,0)<n(e2,0) And n(e2,0)<n(e3,0) And n(e3,0)<n(e4,0) And n(e4,0)<n(e5,0) And n(e5,0)<n(e6,0) Then
                        oExcel.ActiveSheet.Cells(r1,2).Value = n(e1,0)
                        oExcel.ActiveSheet.Cells(r1,3).Value = n(e2,0)
                        oExcel.ActiveSheet.Cells(r1,4).Value = n(e3,0)
                        oExcel.ActiveSheet.Cells(r1,5).Value = n(e4,0)
                        oExcel.ActiveSheet.Cells(r1,6).Value = n(e5,0)
                        oExcel.ActiveSheet.Cells(r1,7).Value = n(e6,0)
                        r1=r1+1
                        End If
                    Next
                Next 
            Next 
        next
    next
NextoExcel.save 
oExcel.quit 
oExcel.Workbooks.Close
MsgBox "ok"用目前这个算法,程序执行起来非常慢,请问如何能提高算法的效率?

解决方案 »

  1.   

    回复楼上,我编辑错了,n是一位数组,正确的代码应该是Dim n(9),sum
    Dim e1,e2,e3,e4,e5,e6
    Dim oExcel,oWb,oSheet,oPathoPath = createobject("wscript.shell").currentdirectory
    Set oExcel= CreateObject("Excel.Application") 
    Set oWb = oExcel.Workbooks.Open(oPath&"\nums.xls") 
    Set oSheet = oWb.Sheets("Sheet1")For i=0 To 9
        n(i) = i
    Next sum = 10For e6=0 To 9
        For e5=0 To 9
            For e4=0 To 9
                For e3=0 To 9
                    For e2=0 To 9
                        For e1=0 To 9
                            If n(e1)+n(e2)+n(e3)+n(e4)+n(e5)+n(e6) = sum And n(e1)<n(e2) And n(e2)<n(e3) And n(e3)<n(e4) And n(e4)<n(e5) And n(e5)<n(e6) Then
                            oExcel.ActiveSheet.Cells(r1,2).Value = n(e1)
                            oExcel.ActiveSheet.Cells(r1,3).Value = n(e2)
                            oExcel.ActiveSheet.Cells(r1,4).Value = n(e3)
                            oExcel.ActiveSheet.Cells(r1,5).Value = n(e4)
                            oExcel.ActiveSheet.Cells(r1,6).Value = n(e5)
                            oExcel.ActiveSheet.Cells(r1,7).Value = n(e6)
                            r1=r1+1
                            End If
                        Next
                    Next 
                Next 
            next
        next
    NextoExcel.save 
    oExcel.quit 
    oExcel.Workbooks.Close
    MsgBox "ok"
    ps:csdn的论坛真奇怪,不能编辑自己的楼层,也不能指定楼层回复
      

  2.   

    VBS是解释执行,别指望运行有多快了
      

  3.   

    既然 n(i)=i,那么n(e)=e
    又要求:n(e1)+n(e2)+n(e3)+n(e4)+n(e5)+n(e6)=sum 
    即:e1+e2+e3+e4+e5+e6=sumFor e6=9 To 0 step -1
    For e5=sum-e6 To 0 step-1
    For e4=sum-e6-e5 to 0 step-1
    For e3=sum-e6-e5-e4 To 0 step-1
    For e2=sum-e6-e5-e4 To 0 step-1
    e1=sum-e6-e5-e4-e3-e2
    If e1<e2  And e2< e3 And e3 < e4 And e4 < e5 And  e5 <e6 Then
    oExcel.ActiveSheet.Cells(r1,2).Value=n(e1)
    oExcel.ActiveSheet.Cells(r1,3).Value=n(e2)
    oExcel.ActiveSheet.Cells(r1,4).Value=n(e3)
    oExcel.ActiveSheet.Cells(r1,5).Value=n(e4)
    oExcel.ActiveSheet.Cells(r1,6).Value=n(e5)
    oExcel.ActiveSheet.Cells(r1,7).Value=n(e6)
    r1=r1+1
    End If
    Next
    Next
    Next
    next
    Next