VB随机数按顺序排列
随机生成6个1到33的互不重复整数,并按从小到大的顺序显示在textbox里面,小于10的要用0填补成两位(比如01)...
随机生成6个1到33的互不重复整数,并按从小到大的顺序显示在textbox里面,小于10的要用0填补成两位(比如01)
展开
1个回答
展开全部
Private Sub Command1_Click()
arr = GetRndNotRepeat(1, 33, 6)
BubbleSort arr
Text1.Text = ""
For i = 0 To 5
Text1.Text = Text1.Text & " " & Format(arr(i), "00")
Next i
End Sub
Public Sub Swap(ByRef a, ByRef b)
'交换
Dim t
t = a: a = b: b = t
End Sub
Public Sub BubbleSort(ByRef a, Optional ByVal Left, Optional ByVal Right)
'起泡排序
'基本思想是:比较相邻的两个记录,逆序则交换。
'这样的做法导致小的关键码一层层的浮上来,因此得名。
Dim i As Integer, j As Integer
If IsMissing(Left) Then Left = LBound(a)
If IsMissing(Right) Then Right = UBound(a)
For i = Left To Right - 1
For j = Right To i + 1 Step -1
If a(j - 1) > a(j) Then Swap a(j - 1), a(j)
Next j
Next i
End Sub
Public Function GetRndNotRepeat(ByVal NumMin As Integer, ByVal NumMax As Integer, ByVal n As Integer)
'编制:xsfhlzh
'功能:取NumMin到NumMax间的n个随机整数
If n > NumMax - NumMin + 1 Then Exit Function
Dim arr()
ReDim arr(n - 1)
Dim b() As Boolean
ReDim b(NumMax - NumMin) '取数标志
Dim x As Integer, y As Integer
Randomize
For i = 0 To n - 1
Do '找到x的位置,y表示x在取数标志数组的位置
x = Int(Rnd * (NumMax - NumMin + 1)) + NumMin
y = x - NumMin
Loop While b(y)
b(y) = True
arr(i) = x '找到未取的数,并放入数组,设置标志位
Next i
GetRndNotRepeat = arr
End Function
arr = GetRndNotRepeat(1, 33, 6)
BubbleSort arr
Text1.Text = ""
For i = 0 To 5
Text1.Text = Text1.Text & " " & Format(arr(i), "00")
Next i
End Sub
Public Sub Swap(ByRef a, ByRef b)
'交换
Dim t
t = a: a = b: b = t
End Sub
Public Sub BubbleSort(ByRef a, Optional ByVal Left, Optional ByVal Right)
'起泡排序
'基本思想是:比较相邻的两个记录,逆序则交换。
'这样的做法导致小的关键码一层层的浮上来,因此得名。
Dim i As Integer, j As Integer
If IsMissing(Left) Then Left = LBound(a)
If IsMissing(Right) Then Right = UBound(a)
For i = Left To Right - 1
For j = Right To i + 1 Step -1
If a(j - 1) > a(j) Then Swap a(j - 1), a(j)
Next j
Next i
End Sub
Public Function GetRndNotRepeat(ByVal NumMin As Integer, ByVal NumMax As Integer, ByVal n As Integer)
'编制:xsfhlzh
'功能:取NumMin到NumMax间的n个随机整数
If n > NumMax - NumMin + 1 Then Exit Function
Dim arr()
ReDim arr(n - 1)
Dim b() As Boolean
ReDim b(NumMax - NumMin) '取数标志
Dim x As Integer, y As Integer
Randomize
For i = 0 To n - 1
Do '找到x的位置,y表示x在取数标志数组的位置
x = Int(Rnd * (NumMax - NumMin + 1)) + NumMin
y = x - NumMin
Loop While b(y)
b(y) = True
arr(i) = x '找到未取的数,并放入数组,设置标志位
Next i
GetRndNotRepeat = arr
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询