VB 大量数据的输出
我想做一个类似下面这样的功能
6 个数分别为r1,r2,r3,r4,r5,r6 r1<r2<r3<r4<r5<r6
r1 从1取至60
r2 从3取至70
r3 从7取至78
r4 从11取至87
r5 从15 取至97
r6 从20 取至 100
然后找出满足r1+r2+r3+r4+r5+r6=333 的所有组合
可能 的组合数可能会有几万,十几万,统计组合数的话比较容易,两三分钟就算出来了,但要把所有满足的组合都输出的话就会运行老长时间,有没有什么优化或者处理的方法。输出到TXT 或EXCEL 都行
[解决办法]
除了6层循环外,好像没什么其它办法。超级计算机的话也就几秒钟。
[解决办法]
定义一个数组,循环的时候写入数组。循环结束后,一次性写入excel。东方兄已经在我的帖子里面给出了方法。请参考http://bbs.csdn.net/topics/390309082第十七楼……
[解决办法]
这个应该会快点
Private Sub Command3_Click()
Dim R1 As Long, R2 As Long, R3 As Long, R4 As Long, R5 As Long, R6 As Long
Dim S2 As Long, S3 As Long, S4 As Long, S5 As Long
For R1 = 1 To 60
For R2 = R1 + 1 To 70
S2 = R1 + R2
For R3 = R2 + 1 To 78
S3 = S2 + R3
For R4 = R3 + 1 To 87
S4 = S3 + R4
For R5 = R4 + 1 To 98
S5 = S4 + R5
For R6 = R5 + 1 To 100
If S5 + R6 = 333 Then
Debug.Print R1, R2, R3, R4, R5, R6
End If
Next
Next
Next
Next
Next
Next
End Sub
Private Sub Command1_Click()
Dim R1 As Long, R2 As Long, R3 As Long, R4 As Long, R5 As Long, R6 As Long
Dim S2 As Long, S3 As Long, S4 As Long, S5 As Long, iLong As Long, sOut() As String
ReDim sOut(0)
Debug.Print Time
For R1 = 1 To 60
For R2 = R1 + 1 To 70
S2 = R1 + R2
For R3 = R2 + 1 To 78
S3 = S2 + R3
For R4 = R3 + 1 To 87
S4 = S3 + R4
For R5 = R4 + 1 To 98
S5 = S4 + R5
For R6 = R5 + 1 To 100
If S5 + R6 = 333 Then
sOut(UBound(sOut)) = R1 & Chr(9) & R2 & Chr(9) & R3 & Chr(9) & R4 & Chr(9) & R5 & Chr(9) & R6
ReDim Preserve sOut(UBound(sOut) + 1)
If UBound(sOut) > 500000 Then GoTo writ
End If
Next
Next
Next
Next
Next
Next
writ: Debug.Print Time
Open App.Path & "/test.xls" For Append As 1#
For iLong = 0 To UBound(sOut) - 1
Print #1, sOut(iLong) '
Next
Close 1#
Debug.Print Time
End Sub