Function GYS(lNum1 As Long, lNum2 As Long) As Long
創(chuàng)新互聯(lián)公司專注于沿河企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。沿河網(wǎng)站建設(shè)公司,為沿河等地區(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
Dim i As Long
For i = lNum1 To 1 Step -1
If lNum1 Mod i = 0 And lNum2 Mod i = 0 Then
GYS = i
Exit For
End If
Next
End Function
Function GBS(lNum1 As Long, lNum2 As Long) As Long
Do While i = False
s = s + 1
i = s Mod lNum1 = 0 And s Mod lNum2 = 0
Loop
GBS = s
End Function
Private Sub Command1_Click()
Dim m As Long, n As Long
m = Val(InputBox("輸入m"))
n = Val(InputBox("輸入n"))
Print "輸入的兩個(gè)數(shù)是 " m "和" n
Print "它們的最大公約數(shù)是";
If m n Then
Print GYS(m, n)
Else
Print GYS(n, m)
End If
Print "它們的最小公倍數(shù)是";
Print GBS(m, n)
End Sub
最大公約數(shù)
m = InputBox("輸入第一個(gè)自然數(shù)")
n = InputBox("輸入第二個(gè)自然數(shù)")
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
End if
MsgBox ("最大公約數(shù)為" n)
最小公倍數(shù)
Dim a%, b%, m%
a = InputBox("第一個(gè)數(shù)")
b = InputBox("第二個(gè)數(shù)")
For m = b To 1 Step -1
If a Mod m = 0 And b Mod m = 0 Then
Exit For
End If
Next
m = a * b / m
MsgBox "最小公倍數(shù)是" + CStr(m)
Private Sub Form_Click()
Dim m As Long, n As Long
m = Val(InputBox("請(qǐng)輸入第一個(gè)數(shù):"))
n = Val(InputBox("請(qǐng)輸入第二個(gè)數(shù):"))
If m 1 Or n 1 Then MsgBox "兩個(gè)數(shù)均需為正整數(shù)!": Exit Sub
MsgBox "最大公約數(shù)是" MaxGys(m, n) vbCrLf "最小公倍數(shù)是" MinGbs(m, n)
End Sub
Rem 求兩個(gè)數(shù)的最大公約數(shù)
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 '輾轉(zhuǎn)相除法
K = X Mod Y: If K = 0 Then Exit Do
X = Y: Y = K
Loop
MaxGys = Y
End Function
Rem 求兩個(gè)數(shù)的最小公倍數(shù)
Private Function MinGbs(A As Long, B As Long)
MinGbs = A * B / MaxGys(A, B)
End Function
最大公約數(shù)
do until a=b
if ab then
a=a-b
elseif ba then
b=b-a
end if
loop
msgbox (a)
最小公倍數(shù)
c = a
22:
If a Mod b = 0 Or b Mod a = 0 Then
If a b Then
MsgBox(a)
Else
MsgBox(b)
End If
Else
a = a + c
GoTo 22
End If
輾轉(zhuǎn)相乘法是什么玩意?
Private Sub Command1_Click()
dim m,n,r ,min as integer
m=text1.text
n=text2.text
do
r=m mod n
m=n
n=r
loop until r=0
text3.text=m
min=val(text1.text)*val(text2.text)/m
text4.text=min
end if
(m是指第一個(gè)數(shù),n是指第二個(gè)數(shù),r是個(gè)參數(shù),min是指最小公倍數(shù),希望你能看懂)
全部程序如下:
function gys(a as integer,b as integer) as integer
dim i as integer,j as integer,r as integer
i=a
j=b
do while i mod j=0
r=i mod j
i=j
j=r
loop
gys=j
end function
function gbs(dim a as integer,b as integer) as integer
dim r as integer
r=gys(a,b)
gbs=a*b/r
end function
Private Sub form1_Click()
dim a as integer,b as integer,c as integer,d as integer
a= Val(inputbox("輸入第一個(gè)整數(shù)","輸入",50))
b= Val(inputbox("輸入第二個(gè)整數(shù)","輸入",75))
c=gys(a,b)
d=gbs(a, b)
print a;"與";b;"的最大公約數(shù)是:";c
print a;"與";b;"的最大公倍數(shù)是:";d
End Sub
就是這些。
有什么問題請(qǐng)留言。