VB求最大公约数的两个例子
VB求最大公约数的两个算法
Private Sub Command1_Click()
Dim a As Long, b As Long
a = InputBox("请输入要求最大公约数的整数", " 求两数的最大公约数:step1", 0)
b = InputBox("请输入要求最大公约数的整数", " 求两数的最大公约数:step2", 0)
Print "step1>>>整数1 : "a
Print "step2>>>整数2 : "; b
Do While a <> b
If a > b Then
a = a - b
Else b = b - a
End If Loop
Print "step3:执行步数:"; i
Print Print "结果: 最大公约数为 》》》"; a
Print "______________________________________"
End Sub
Rem ?声明:非盈利,切勿用于商业用途。制作者:Totoo
Rem ?功能:计算两数的最大公约数
Rem ?环境:windows
Rem ?类型:开源 'writed by totoo'
Private Sub Command2_Click()
Dim x As Long, y As Long, m As Long, n As Long, r As Long, i As Long
i = 1 x = InputBox("请输入要求最大公约数的整数", " 求两数的最大公约数:step1", 0)
y = InputBox("请输入要求最大公约数的整数", " 求两数的最大公约数:step2", 0)
Print "step1>>>整数1 : "; x
Print "step2>>>整数2 : "; y
If x - y > 0 Then n = y
Else n = x x = y y = n r = x - y
End If
Do While r <> 0
If n - r > 0 Then m = n n = r
Else m = r End If
r = m - n i = i + 1 Loop
Print "step3:执行步数:"; i
Print Print "结果: 最大公约数为 》》》"; n
Print "______________________________________"
End Sub