|
'修改了一下,10*10速度非常快,20就开始卡了
Option Explicit
Sub test()
Dim i, j, k, a, n, t, key, b, s, dt
dt = Timer
ReDim arr(1 To 10, 1 To 10), dic(1 To 2)
For i = 1 To UBound(dic)
Set dic(i) = CreateObject("scripting.dictionary")
Next
For i = 1 To UBound(arr, 2): arr(1, i) = i: Next
Randomize
For i = 1 To UBound(arr, 2)
n = Int(Rnd * UBound(arr, 1)) + 1
t = arr(1, i): arr(1, i) = arr(1, n): arr(1, n) = t
Next
For j = 1 To UBound(arr, 2)
dic(1).RemoveAll: n = 1
For i = 1 To UBound(arr, 1)
If i <> arr(1, j) Then dic(1)(i) = vbNullString
Next
For Each key In dic(1).keys
n = n + 1: arr(n, j) = key
Next key, j
For j = 1 To UBound(arr, 2)
Randomize
For i = 2 To UBound(arr, 1)
dic(1).RemoveAll
For b = j - 1 To 1 Step -1
dic(1)(arr(i, b)) = vbNullString
Next
For a = i - 1 To 1 Step -1
dic(1)(arr(a, j)) = vbNullString
Next
dic(2).RemoveAll
For a = 1 To 10
If Not dic(1).exists(a) Then dic(2)(a) = vbNullString
Next
If dic(2).Count > 0 Then
t = dic(2).keys
s = Int(Rnd * (UBound(t) + 1))
arr(i, j) = t(s)
Else
i = 1
End If
Next i, j
[a1].Resize(UBound(arr, 1), UBound(arr, 2)) = arr
Debug.Print Timer - dt
End Sub |
|