VB求两个数的最大公约数和最小公倍数
答案:2 悬赏:0 手机版
解决时间 2021-03-17 01:39
- 提问者网友:温旧梦泪无声
- 2021-03-16 17:34
VB求两个数的最大公约数和最小公倍数
最佳答案
- 五星知识达人网友:千杯敬自由
- 2019-10-05 21:24
Private Sub Command1_Click()
Dim S As Long
S = YueOrBei(24, 32)
MsgBox "24 和 32 的最大公约数是:" & S, vbInformation
S = YueOrBei(24, 32, True)
MsgBox "24 和 32 的最小公倍数是:" & S, vbInformation
End Sub
Private Function YueOrBei(J1 As Long, J2 As Long, Optional IsBei As Boolean) As Long 'IsBei=True 返回公倍数(最小),否则返回公约数(最大)
Dim S As Long, S1 As Long, S2 As Long
S1 = J1: S2 = J2
Do
S = S1 Mod S2
If S = 0 Then YueOrBei = S2: Exit Do
S1 = S2: S2 = S
Loop
If IsBei Then YueOrBei = J1 * J2 / YueOrBei
End Function另一种方法
Private Sub Form_Click()
m1 = InputBox("输入m")
n1 = InputBox("输入n")
If m1 > n1 Then '为了求最小公倍数,增加m,n变量
m = m1: n = n1
Else
m = n1: n = m1
End If 'm>n
r = m Mod n
Do While r <> 0
m = n
n = r
r = m Mod n
Loop
Print m1; ","; n1; "的最大约数为"; n
Print "最小公倍数=", m1 * n1 / n
End Sub
Dim S As Long
S = YueOrBei(24, 32)
MsgBox "24 和 32 的最大公约数是:" & S, vbInformation
S = YueOrBei(24, 32, True)
MsgBox "24 和 32 的最小公倍数是:" & S, vbInformation
End Sub
Private Function YueOrBei(J1 As Long, J2 As Long, Optional IsBei As Boolean) As Long 'IsBei=True 返回公倍数(最小),否则返回公约数(最大)
Dim S As Long, S1 As Long, S2 As Long
S1 = J1: S2 = J2
Do
S = S1 Mod S2
If S = 0 Then YueOrBei = S2: Exit Do
S1 = S2: S2 = S
Loop
If IsBei Then YueOrBei = J1 * J2 / YueOrBei
End Function另一种方法
Private Sub Form_Click()
m1 = InputBox("输入m")
n1 = InputBox("输入n")
If m1 > n1 Then '为了求最小公倍数,增加m,n变量
m = m1: n = n1
Else
m = n1: n = m1
End If 'm>n
r = m Mod n
Do While r <> 0
m = n
n = r
r = m Mod n
Loop
Print m1; ","; n1; "的最大约数为"; n
Print "最小公倍数=", m1 * n1 / n
End Sub
全部回答
- 1楼网友:独行浪子会拥风
- 2019-10-15 02:57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
privatesubcommand1_click()
dims aslong
s = yueorbei(24, 32)
msgbox "24 和 32 的最大公约数是:"& s, vbinformation
s = yueorbei(24, 32, true)
msgbox "24 和 32 的最小公倍数是:"& s, vbinformation
endsub
privatefunctionyueorbei(j1 aslong, j2 aslong, optionalisbei asboolean) aslong'isbei=true 返回公倍数(最小),否则返回公约数(最大)
dims aslong, s1 aslong, s2 aslong
s1 = j1: s2 = j2
do
s = s1 mods2
ifs = 0 thenyueorbei = s2: exitdo
s1 = s2: s2 = s
loop
ifisbei thenyueorbei = j1 * j2 / yueorbei
endfunction
另一种方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
privatesubform_click()
m1 = inputbox("输入m")
n1 = inputbox("输入n")
ifm1 > n1 then'为了求最小公倍数,增加m,n变量
m = m1: n = n1
else
m = n1: n = m1
endif'm>n
r = m modn
dowhiler 0
m = n
n = r
r = m modn
loop
print m1; ","; n1; "的最大约数为"; n
print "最小公倍数=", m1 * n1 / n
endsub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯