|
楼主 |
发表于 2017-4-15 14:49
|
显示全部楼层
呵呵,钻一下牛角尖。
就你的题目例子来说,下面算法可以生成很多满足条件的随机组合。
- Sub test2()
- Dim a&(), b&(), dic, h&, h1&, h2&, i&, j&, m&, n&, r1&, r2&, t&, tms#
- tms = Timer
-
- h = 590: h1 = 10: h2 = 70
- m = 61: n = 21
-
- ReDim a(n - 1), b(h1 To h2)
- t = h
- For i = 0 To n - 1
- a(i) = i + h1: b(a(i)) = 1: t = t - a(i)
- Next
- For i = n - 1 To 0 Step -1
- If t Then t = t + a(i): b(a(i)) = 0: If t > i + h2 - n + 1 Then a(i) = i + h2 - n + 1: b(a(i)) = 1: t = t - a(i) Else a(i) = t: b(t) = 1: Exit For
- Next
-
- For j = 1 To 10 '需要生成的组合次数
- For i = 1 To n * m
- r1 = Int(Rnd * n): r2 = Int(Rnd * (n - 1) + r1 + 1) Mod n
- If h1 + h2 < a(r1) + a(r2) Then t = h2 - a(r2) Else t = a(r1) - h1
- t = Int(Rnd * (t + 1))
- If b(a(r1) - t) + b(a(r2) + t) = 0 Then
- If a(r1) - t <> a(r2) + t Then
- b(a(r1)) = 0: b(a(r2)) = 0
- a(r1) = a(r1) - t: a(r2) = a(r2) + t
- b(a(r1)) = 1: b(a(r2)) = 1
- End If
- End If
- Next
- Cells(1, j).Resize(n) = WorksheetFunction.Transpose(a)
- Next
-
- MsgBox Format(Timer - tms, "0.000s")
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|