求:编vb小程序
发布网友
发布时间:2022-04-27 04:02
我来回答
共4个回答
热心网友
时间:2022-06-25 21:43
'判断是否为素数的函数
Function IsSuShu(ByVal shuzi) As Boolean
Dim i As Long
Dim b As Boolean
b = True
For i = 2 To shuzi - 1
If shuzi Mod i = 0 Then
b = False
Exit For
End If
Next
IsSuShu = b
End Function
Private Sub Command1_Click()
Dim A(1 To 80)
Dim JiShu(1 To 80)
Dim OuShu(1 To 80)
Dim SumSuShu '素数和
Dim i, j, k
Randomize '初始化随即种子,避免每次产生的随机数相同
For i = 1 To 80
A(i) = Int(Rnd() * 90) + 10 '随机产生10 到99的整数并存到数组A中
Next
Cls
Print "随机产生的80个10到99的整数为:"
For i = 0 To 7
Print A(i * 10 + 1); A(i * 10 + 2); A(i * 10 + 3); A(i * 10 + 4); A(i * 10 + 5); A(i * 10 + 6); A(i * 10 + 7); A(i * 10 + 8); A(i * 10 + 9); A(i * 10 + 10)
Next
For i = 1 To 80
If IsSuShu(A(i)) = True Then '随机产生10 到99的整数并存到数组A中
SumSuShu = SumSuShu + A(i)
End If
Next
Print '打印一行空行
Print "这些数中的所有素数之和为:" & SumSuShu
For i = 1 To 80
If A(i) Mod 2 = 0 Then
k = k + 1
OuShu(k) = A(i)
Else
j = j + 1
JiShu(j) = A(i)
End If
Next
Print
Print "其中的奇数有:"
Dim xx
If j / 10 = Int(j / 10) Then
xx = Int(j / 10)
Else
xx = Int(j / 10 + 1)
End If
For i = 0 To xx - 1
Print JiShu(i * 10 + 1); JiShu(i * 10 + 2); JiShu(i * 10 + 3); JiShu(i * 10 + 4); JiShu(i * 10 + 5); JiShu(i * 10 + 6); JiShu(i * 10 + 7); JiShu(i * 10 + 8); JiShu(i * 10 + 9); JiShu(i * 10 + 10)
Next
Print
Print "其中的偶数有:"
If k / 10 = Int(k / 10) Then
xx = Int(k / 10)
Else
xx = Int(k / 10 + 1)
End If
For i = 0 To xx - 1
Print OuShu(i * 10 + 1); OuShu(i * 10 + 2); OuShu(i * 10 + 3); OuShu(i * 10 + 4); OuShu(i * 10 + 5); OuShu(i * 10 + 6); OuShu(i * 10 + 7); OuShu(i * 10 + 8); OuShu(i * 10 + 9); OuShu(i * 10 + 10)
Next
End Sub
热心网友
时间:2022-06-25 21:44
Option Base 1
Dim a%(80), i%, k%, t%, b%(80), c%(80)
Private Sub Command1_Click()
Me.Cls
Dim j As Integer
Randomize
t = 1
k = 1
For i = 1 To 80
a(i) = Int(Rnd * 90) + 10
If a(i) Mod 2 = 1 Then
b(k) = a(i)
k = k + 1
End If
If IsPrimeNum(a(i)) Then
c(t) = a(i)
t = t + 1
End If
Next i
Print "全部"
For i = 1 To 80
Print a(i);
If i Mod 10 = 0 Then Print
Next i
SortArray b, k - 1
Print "奇数"
For i = 1 To k - 1
Print b(i);
If i Mod 10 = 0 Then Print
Next i
Print
SortArray c, t - 1
Print "素数"
For i = 1 To t - 1
Print c(i);
If i Mod 10 = 0 Then Print
Next i
End Sub
Private Function IsPrimeNum(V As Integer) As Boolean
Dim i As Integer
Dim flag As Boolean
flag = True
For i = 2 To V - 1
If V Mod i = 0 Then
flag = False
Exit For
End If
Next i
IsPrimeNum = flag
End Function
Private Sub SortArray(arr() As Integer, ByVal size As Integer)
Dim i As Integer
Dim j As Integer
Dim t As Integer
For i = 1 To size
For j = i + 1 To size
If arr(i) > arr(j) Then t = arr(i): arr(i) = arr(j): arr(j) = t
Next j
Next i
End Sub
'添加一个按钮就可以搞定
热心网友
时间:2022-06-25 21:44
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim OddCount As Integer
Dim EvenCount As Integer
Dim PrimeCount As Integer
Dim Temp As Integer
Dim SumPrime As Long
Dim ArrRnd(80) As Integer
Dim ArrOdd(80) As Integer
Dim ArrEven(80) As Integer
Dim ArrPrime(80) As Integer
Randomize
For i = 0 To 79
ReCreate:
ArrRnd(i) = Int((Rnd * 90) + 10)
For k = 0 To i - 1
If ArrRnd(k) = ArrRnd(i) Then
GoTo ReCreate
End If
Next k
If Prime(ArrRnd(i)) = True Then
ArrPrime(PrimeCount) = ArrRnd(i)
SumPrime = SumPrime + ArrRnd(i)
PrimeCount = PrimeCount + 1
End If
If ArrRnd(i) Mod 2 <> 0 Then
ArrOdd(OddCount) = ArrRnd(i)
For j = OddCount To 1 Step -1
If ArrOdd(j) > ArrOdd(j - 1) Then
Temp = ArrOdd(j)
ArrOdd(j) = ArrOdd(j - 1)
ArrOdd(j - 1) = Temp
End If
Next j
OddCount = OddCount + 1
Else
ArrEven(EvenCount) = ArrRnd(i)
For j = EvenCount To 1 Step -1
If ArrEven(j) < ArrEven(j - 1) Then
Temp = ArrEven(j)
ArrEven(j) = ArrEven(j - 1)
ArrEven(j - 1) = Temp
End If
Next j
EvenCount = EvenCount + 1
End If
Next i
Print "随机数:"
For i = 0 To 79
If i Mod 10 <> 9 Then
Print ArrRnd(i);
Else
Print ArrRnd(i)
End If
Next i
Print
Print "奇数(从大到小):"
For i = 0 To OddCount - 1
If i Mod 10 <> 9 Then
Print ArrOdd(i);
Else
Print ArrOdd(i)
End If
Next i
Print
Print "偶数(从小到大):"
For i = 0 To EvenCount - 1
If i Mod 10 <> 9 Then
Print ArrEven(i);
Else
Print ArrEven(i)
End If
Next i
Print
Print "素数:"
For i = 0 To PrimeCount - 1
If i Mod 10 <> 9 Then
Print ArrPrime(i);
Else
Print ArrPrime(i)
End If
Next i
Print
Print "素数个数:" & PrimeCount & " 素数和:" & SumPrime
End Sub
Private Function Prime(ByVal Number As Integer) As Boolean
If Number Mod 2 = 0 Then
Prime = False
Exit Function
Else
For i = 3 To Int(Sqr(Number)) Step 1
If Number Mod i = 0 Then
Prime = False
Exit Function
End If
Next i
End If
Prime = True
End Function
热心网友
时间:2022-06-25 21:45
学习