|
楼主 |
发表于 2020-3-7 21:44
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
谢谢老师的答复,知道了老师的逻辑思维,再理解代码应该容易一些,本来想最后研究你的代码的,因为看起来最复杂。
以下代码,是按单个元素依次置换的,整行,整列置换应该怎么写
Sub 验证()
Dim m, arr(), c, i, k, r, x, t
m = 15
Cells(1, 1).Resize(16, 16).Clear
ReDim arr(1 To m, 1 To m)
For i = 1 To m
For k = 1 To m
If i + k - 1 <= m Then
arr(i, k) = i + k - 1
Else
arr(i, k) = i + k - 1 - m
End If
Next k
Next i
For c = 1 To m '**************按列随机交换
x = Int(Rnd * (m) + 1)
For r = 1 To m
t = arr(r, c): arr(r, c) = arr(r, x): arr(r, x) = t
Next r
Next c
For r = 1 To m '****************按行随机交换
x = Int(Rnd * (m) + 1)
For c = 1 To m
t = arr(r, c): arr(r, c) = arr(x, c): arr(x, c) = t
Next c
Next r
Cells(1, 1).Resize(m, m) = arr
End Sub
|
|