求做一道VB编程题

来源:百度知道 编辑:UC知道 时间:2024/05/15 19:58:58
从键盘上输入两个正整数M和N,求最大公因子。

Private Sub Command1_Click()
Dim S1 As Long, S2 As Long, S As Long

S1 = InputBox("请输入第一个数:", , 8)
S2 = InputBox("请输入第二个数:", , 10)
S = YueOrBei(S1, S2)
MsgBox S1 & "和" & S2 & "的最大公因数是:" & S
End Sub

Private Function YueOrBei(ByVal S1 As Long, ByVal S2 As Long, Optional IsBei As Boolean) As Long
'IsBei=True 返回公倍数(最小),否则返回公约数(最大)
Dim S As Long, Ji As Long

If IsBei Then Ji = S1 * S2 '为求最小公倍数做准备

'求余法
Do
S = S1 Mod S2
If S = 0 Then YueOrBei = S2: Exit Do
S1 = S2: S2 = S
Loop
If IsBei Then YueOrBei = Ji / YueOrBei
End Function

dim x,i as integer
x=m
if m>n then x=n
for i=0 to x-1
if (m mod (n-i))=0 and (n mod (n-i))=0 then exit for
next i
x=n-i
print x

Function GCD(a A