在Text1和text2中填入两个数,单击“计算”按钮,求这两个数的最大公约数和最小公倍数。
展开全部
Private Sub Command1_Click()
Dim m As Long, n As Long
m = Val(Text1.Text)
n = Val(Text2.Text)
If m < 1 Or n < 1 Then MsgBox "两个数均需为正整数!": Exit Sub
MsgBox "最大公约数是" & MaxGys(m, n) & vbCrLf & "最小公倍数是" & MinGbs(m, n)
End Sub
Rem 求两个数的最大公约数
Private Function MaxGys(A As Long, B As Long)
Dim X As Long, Y As Long, K As Long
X = IIf(A >= B, A, B) 'x存入最大值
Y = IIf(A <= B, A, B) 'y 存入最小值
Do '辗转相除法
K = X Mod Y: If K = 0 Then Exit Do
X = Y: Y = K
Loop
MaxGys = Y
End Function
Rem 求两个数的最小公倍数
Private Function MinGbs(A As Long, B As Long)
MinGbs = A * B / MaxGys(A, B)
End Function
Dim m As Long, n As Long
m = Val(Text1.Text)
n = Val(Text2.Text)
If m < 1 Or n < 1 Then MsgBox "两个数均需为正整数!": Exit Sub
MsgBox "最大公约数是" & MaxGys(m, n) & vbCrLf & "最小公倍数是" & MinGbs(m, n)
End Sub
Rem 求两个数的最大公约数
Private Function MaxGys(A As Long, B As Long)
Dim X As Long, Y As Long, K As Long
X = IIf(A >= B, A, B) 'x存入最大值
Y = IIf(A <= B, A, B) 'y 存入最小值
Do '辗转相除法
K = X Mod Y: If K = 0 Then Exit Do
X = Y: Y = K
Loop
MaxGys = Y
End Function
Rem 求两个数的最小公倍数
Private Function MinGbs(A As Long, B As Long)
MinGbs = A * B / MaxGys(A, B)
End Function
追问
- -!那个。。有没有简单点的。太长了。抄不下
展开全部
Private Sub Command1_Click()
Dim n%, m%, nm%, r%
m = Text1.Text
n = Text2.Text
nm = n * m
If m < n Then t = m: m = n: n = t
r = m Mod n
Do While (r <> 0)
m = n
n = r
r = m Mod n
Loop
Print "最大公约数=", n
Print "最小公倍数=", nm / n
End Sub
这样够简单的吧
Dim n%, m%, nm%, r%
m = Text1.Text
n = Text2.Text
nm = n * m
If m < n Then t = m: m = n: n = t
r = m Mod n
Do While (r <> 0)
m = n
n = r
r = m Mod n
Loop
Print "最大公约数=", n
Print "最小公倍数=", nm / n
End Sub
这样够简单的吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询