导航:首页 > 编程语言 > 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程序最小公倍数相关的资料

热点内容
欠费多少充多少为啥还用不了数据 浏览:607
苹果7如何使用万能钥匙 浏览:254
微信文件传送电脑 浏览:600
什么app可以解压百度云rar 浏览:627
苹果6sp换壳 浏览:956
海盗船k70rgb灯光配置文件 浏览:336
linuxfsstat 浏览:926
电脑文件有个锁 浏览:441
ps多张pdf文件夹 浏览:2
怎样压缩文件能传到qq邮箱 浏览:923
南昌房管局网站怎么查备案 浏览:884
如何设置ipad下载密码 浏览:458
ae信号干扰教程 浏览:548
电脑之前删掉的文件怎么找 浏览:805
索尼z1刷什么系统升级 浏览:466
自创字体怎么编程序 浏览:381
成都的苹果售后服务电话号码 浏览:698
文件放笔记本哪个盘 浏览:745
starbound星球文件格式 浏览:137
javathis和super 浏览:782

友情链接