导航:首页 > 编程语言 > vb程序最小公倍数

vb程序最小公倍数

发布时间:2024-09-13 12:33:36

① 用VB语言编写程序,求两个正整数的最小公倍数

Private Sub Command1_Click()
Dim i As Long, j As Long, k As Long
Dim A As Long, B As Long

A = Val(InputBox("请输入第一个正整数", "输入"))
B = Val(InputBox("请输入第二个正整数", "输入"))
i = A
j = B
Do '求得A与B的最大公约数
DoEvents
If i > j Then
k = i
i = j
j = k
ElseIf i = j Then
Exit Do
End If
j = j - i
Loop
MsgBox "最小公倍数是" & A * B / i, , "输出"
End Sub

'以下是加强版
Private Sub Command1_Click()
Dim i As Long, j As Long, k As Long
Dim A() As Double, B As Double, C As Double, D As Double, E As Double
Dim STA As String, STB As String

MsgBox "如果数据有效位超过15位,则有可能因位数过多造成计算结果的不准确,请注意输入的数据。", , "提示"
STA = InputBox("请输入正整数个数", "输入")
STA = Trim(STA)
j = Len(STA)
STB = ""
For i = 1 To j
If Asc(Mid(STA, i, 1)) <> 32 Then STB = STB & Mid(STA, i, 1)
Next
STB = Left(STB, 323)
B = Val(STB)
If B < 2 Then
MsgBox "个数输入不正确,退出", , "错误"
Exit Sub
ElseIf B > 2 ^ 31 - 2 Then
MsgBox "个数输入不正确,退出", , "错误"
Exit Sub
Else
B = Int(B)
End If
ReDim A(1 To B)
For i = 1 To B
STA = InputBox("请输入第" & i & "个正整数", "输入")
STA = Trim(STA)
j = Len(STA)
STB = ""
For k = 1 To j
If Asc(Mid(STA, k, 1)) <> 32 Then STB = STB & Mid(STA, k, 1)
Next
STB = Left(STB, 323)
A(i) = Val(STB)
If A(i) < 1 Then
MsgBox "第" & i & "个正整数输入不正确,退出", , "错误"
Exit Sub
Else
A(i) = Int(A(i))
End If
Next
For i = 1 To B - 1
C = A(i)
D = A(i + 1)
Do
DoEvents
If C > D Then
E = C
C = D
D = E
ElseIf C = D Then
Exit Do
End If
D = D - C
Loop
A(i + 1) = A(i) * A(i + 1) / C
Next
MsgBox "最小公倍数是" & A(B), , "输出"
End Sub

② 用VB编程:求两个整数的最小公倍数。

没太多的时间修改,给你个例子:
方法简述:最小公倍数等于两个正整数相乘再除以两个正整数的最大公约数(即a*b/g)。说明求两个正整数最大公约数,自己查找。

vb程序代码如下:

Private Sub Command1_Click()
Dim m As Integer, n As Integer
Dim g As Integer
dim t1 as integer,t2 as integer
m = InputBox("第一个数")
n = InputBox("第二个数")
t1=m 't1,t2是临时变量
t2=n
g = IIf(m < n, m, n)
Do While g > 1
If m Mod g = 0 And n Mod g = 0 Then Exit Do
g = g - 1
Loop
Print m & "与" & n & "的最小公倍数为:" & (t1*t2)/g
End Sub

阅读全文

与vb程序最小公倍数相关的资料

热点内容
微软平板开机密码设置 浏览:978
linux删除一行的快捷键 浏览:269
win10改hosts文件 浏览:354
数据博世怎么样 浏览:411
用tar解包没有那个文件或目录 浏览:307
录歌教程 浏览:604
java小数进制转换 浏览:270
80后qq头像女生 浏览:388
word2013页面颜色 浏览:661
linux系统安装显卡驱动 浏览:243
手机安卓培训机构 浏览:819
英语版本的哈面宝宝 浏览:567
手机动态壁纸教学视频教程 浏览:543
网络摄像机sip 浏览:757
湘潭编程学校哪里好 浏览:389
win10设置桌面小图标怎么去掉吗 浏览:122
网络安全创业 浏览:787
修改linux 浏览:464
如何编程计算机cpu高占用程序 浏览:808
程序员活动策划方案 浏览:130

友情链接