Private Sub Form_Load()
Dim arr(9) As Integer
Dim i As Integer, j As Integer
Dim strOut As String
'随机生成10个数字
Randomize
For i = 0 To UBound(arr)
arr(i) = Int(Rnd * 89) + 10
strOut = strOut &arr(i) &" "
Next
Debug.Print "十个数字为:" &strOut
For i = 0 To UBound(arr)
For j = i + 1 To UBound(arr)
If getMaxCD(arr(i), arr(j)) = 1 Then
Debug.Print arr(i) &"和" &arr(j) &"为互质数"
End If
Next
Next
End Sub
'返回两数的最大公约数
Private Function getMaxCD(ByVal num1 As Long, ByVal num2 As Long) As Long
Dim temp, a, b
If num1 <num2 Then
temp = num1
num1 = num2
num2 = temp
End If
a = num1
b = num2
While b <>0
temp = a Mod b
a = b
庆余咐b = temp
Wend
毁键 誉纯getMaxCD = a
End Function
在窗口上放2个label和1个button,名字不改 下面代猛答誉码放到button的click事件中: Label1.Text = "" Label2.Text = "" Dim num(9) As Integer Dim i As Integer = 0 Dim j As Integer = 0 Dim k As Integer = 0 Randomize() For i = 0 To 9 '数组赋值随机数,并将数全部打印在label11中 num(i) = Rnd() * 90 + 10 Label1.Text = Label1.Text &num(i).ToString &vbCrLf Next For i = 0 To 8 For j = i + 1 To 9 '双层循环,用于进行两个数的 *** 作 '判断num(i)和num(j)的大小,循环到最小的数 If num(i) >= num(j) Then For k = 2 To num(j) '循环判断是否有公约数,有的话跳出循环 If (num(i) Mod k = 0) And (num(j) Mod k = 0) Then Exit For End If Next k If k >num(j) Then '举枯当判断成立时说明上面是循环结束枝段,并不是中间跳出,因此没有公约数,两个数是互质数,然后打印在label2中 Label2.Text = Label2.Text &num(i).ToString &"," &num(j).ToString &vbCrLf End If Else For k = 2 To num(i) If (num(i) Mod k = 0) And (num(j) Mod k = 0) Then Exit For End If Next k If k >num(i) Then Label2.Text = Label2.Text &num(i).ToString &"," &num(j).ToString &vbCrLf End If End If Next j Next i放两个TEXTBOX用来输入,一个按扭.Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
a = Int(Text1)
b = Int(Text2)
For i = 2 To a - 1
If (a Mod i) = 0 Then
If (b Mod i) = 0 Then
MsgBox "这两个数不是互质数。"
Exit Sub
End If
End If
Next i
For i = 2 To b - 1
If (b Mod i) = 0 Then
If (a Mod i) = 0 Then
MsgBox "这滑碧两个数不渣培是互质数如让唯。"
Exit Sub
End If
End If
Next i
MsgBox "这两个数是互质数。"
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)