两个各包含100个元素的数组A和B,A按顺序依次存放1-100这100个数,怎么打乱顺序,将A的这100个数存放到B中
两个各包含100个元素的数组A和B,A按顺序依次存放1-100这100个数,如何打乱顺序,将A的这100个数存放到B中
两个各包含100个元素的数组A和B,A按顺序依次存放1-100这100个数,如何打乱顺序,将A的这100个数存放到B中,使B中存放的这100个数显得毫无规律。
------最佳解决方案--------------------
------其他解决方案--------------------
Private Sub Command1_Click()
Dim i As Integer
List2.Clear
List1.Clear
For i = 1 To 100
List1.AddItem i
Next i
End Sub
Private Sub Command2_Click()
Dim i As Integer, n As Integer
Randomize
Do While List1.ListCount
If List1.ListCount = 1 Then
List2.AddItem List1.List(0)
List1.Clear
Else
n = Int(Rnd() * List1.ListCount + 0.5)
If n >= List1.ListCount Then n = List1.ListCount - 1
List2.AddItem List1.List(n)
List1.RemoveItem n
End If
Loop
End Sub
------其他解决方案--------------------
楼上的方法就可以了
------其他解决方案--------------------
直接用rnd*100+1给数组B赋值是最快的,但是不能保证在数组B内不出现重复的数据.
----ms没说rnd的重复能力.
2L没必要给数组A赋值,A没有用,开始时直接赋值数组B算了
两个各包含100个元素的数组A和B,A按顺序依次存放1-100这100个数,如何打乱顺序,将A的这100个数存放到B中,使B中存放的这100个数显得毫无规律。
------最佳解决方案--------------------
Option Explicit
Sub Main()
Dim A(1 To 100) As Long
Dim B(1 To 100) As Long
Dim lTmp As Long
Dim i As Long
Dim j As Long
For i = 1 To 100
A(i) = i
Next
'复制 A 到 B
For i = 1 To 100
B(i) = A(i)
Next
'洗牌:将 B(i) 与随机的 B(j) 交换
Randomize
For i = 1 To 100
j = Int(Rnd() * 100) + 1
lTmp = B(i)
B(i) = B(j)
B(j) = lTmp
Next
'查看结果
For i = 1 To 100
Debug.Print i, B(i)
Next
End Sub
------其他解决方案--------------------
Private Sub Command1_Click()
Dim i As Integer
List2.Clear
List1.Clear
For i = 1 To 100
List1.AddItem i
Next i
End Sub
Private Sub Command2_Click()
Dim i As Integer, n As Integer
Randomize
Do While List1.ListCount
If List1.ListCount = 1 Then
List2.AddItem List1.List(0)
List1.Clear
Else
n = Int(Rnd() * List1.ListCount + 0.5)
If n >= List1.ListCount Then n = List1.ListCount - 1
List2.AddItem List1.List(n)
List1.RemoveItem n
End If
Loop
End Sub
------其他解决方案--------------------
楼上的方法就可以了
------其他解决方案--------------------
直接用rnd*100+1给数组B赋值是最快的,但是不能保证在数组B内不出现重复的数据.
----ms没说rnd的重复能力.
2L没必要给数组A赋值,A没有用,开始时直接赋值数组B算了