#include <stdlib.h>
int main()
{
int num[100]
int time=0
int numtemp
while (1) //取随机数
{
numtemp = rand()%1000
if (numtemp >= 100 &&numtemp <=999 )
{
num[time] = numtemp
time++
}
if (time == 100)
{
break
}
}
/* for(int i=0i<100i++) //冒泡排序比较方法
{
for (int j=99j>ij--)
{
int temp
if (num[j] <num[j-1])
{
temp = num[j]
num[j] = num[j-1]
num[j-1] = temp
}
}
printf("%d\n",num[i])
}*/
for (int i = 0i <100 - 1i++) //选择排序比较方法
{
int iMin = i
for (int j= ij <100j++)
{
if(num[j] <num[iMin]) iMin = j
}
if (iMin != i)
{
int nTmp = num[iMin]
num[iMin] = num[i]
num[i] = nTmp
}
}
for (int nI=0nI<100nI++)
{
printf("%d\n",num[nI])
}
return 0
}
Private Sub Command1_Click()Dim arr, a
arr = Array(10, 25, 34, 54, 61)
a = GetRndNotRepeat(0, 4, 5)
For i = 0 To 4
Controls("Label" &i + 1).Caption = arr(a(i))
Next i
End Sub
Public Function GetRndNotRepeat(ByVal NumMin As Integer, ByVal NumMax As Integer, ByVal n As Integer)
'作者:xsfhlzh
'功能:取NumMin到NumMax间的n个随机整数
'说明:取数标志数组是Byte,每一位表示NumMin到NumMax间某个数的状态
'示例:
'a = GetRndNotRepeat(1, 10000, 2000)
'If UBound(a) >= 0 Then
'For i = 0 To 1999
'Debug.Print a(i)
'Next i
'End If
Dim arr() As Integer
If n >NumMax - NumMin + 1 Then
ReDim arr(-1 To -1)
Else
ReDim arr(n - 1)
Dim b() As Byte
m = Int((NumMax - NumMin) / 8)
ReDim b(m)
'取数标志
Dim x As Integer
Dim y As Integer
Dim z As Byte
Randomize
For i = 0 To n - 1
Do
'找到x的位置,y表示x在数组的第几个字节,z表示x在该字节的第几位
x = Int(Rnd * (NumMax - NumMin + 1)) + NumMin
y = x - NumMin
z = 2 ^ (y Mod 8)
y = y \ 8
Loop While b(y) And z
b(y) = b(y) Or z
arr(i) = x
'找到未取的数,并放入数组,设置标志位
Next i
End If
GetRndNotRepeat = arr
End Function
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)