VB编程题
1.水仙花数
Private Sub Form_Load() Dim x!, a!, b!, c!
x = InputBox(输入一个三位整数)
a = x \ 100
b = x \ 10 Mod 10
c = x Mod 10
If x = a ^ 3 + b ^ 3 + c ^ 3 Then
MsgBox "是水仙花数"
Else
MsgBox "不是水仙花数"
End If
End Sub
1. Fibonacci函数。
Private Sub Command1_Click() Dim n As Integer
n = Text1
Text2 = fib(n)
End Sub
Function fib(ByVal x As Integer) As Integer
If x = 1 Or x = 2 Then
fib = 1
Else
fib = fib(x - 2) + fib(x - 1)
End If
End Function
Private Sub Command2_Click() Text1 = ""
Text2 = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click() End
End Sub
2. 百元百鸡
Private Sub cmdCalc_Click()
'变量说明
Dim x %,y%,z%
'穷举法计算
For x = 1 To 20
For y = 1 To
z = 100 - x - y
If 5 * x + 3 * y + z / 3 = 100 Then
Text1 = Text1 & CStr(x) & Space(6) & CStr(y) & Space(6) & CStr(z) &
Chr(13) & Chr(10)
End If
Next y
Next x
End Sub
3. 矩阵不靠边元素和
For j = 1 To 5
a(i, j) = Int(Rnd * 90) + 10
Next
Next
For i = 1 To 5
For j = 1 To 5
Picture1.Print a(i, j);
Next
Picture1.Print
Next
End Sub
Private Sub Command2_Click() Text1 = fun1(a)
End Sub
Function fun1(x() As Integer) As Double Dim i As Integer, j As Integer fun1 = 0
For i = LBound(x, 1) To UBound(x, 1)
For j = LBound(x, 2) To UBound(x, 2)
If Not (i = LBound(x, 1) Or i = UBound(x, 1) Or j = LBound(x, 2) Or j = UBound(x, 2))
Then
fun1 = fun1 + x(i, j)
End If
Next
Next
End Function
Private Sub Command3_Click() Text1 = ""
Picture1.Cls
Command1.SetFocus
End Sub
Private Sub Command4_Click() End
End Sub
4. 递归函数
Private Sub Command1_Click() List1.Clear
Dim i As Integer
For i = 1 To 7
List1.AddItem fact(i) Next i
End Sub
Function fact(ByVal n As Double) As Double
If n = 1 Then
fact = 1
Else
fact = n * fact(n - 1) End If
End Function
Private Sub Command2_Click() List1.Clear
End Sub
Private Sub Command3_Click() End
End Sub
5. 回文数
Private Sub Command1_Click() Randomize
Dim i As Integer, x As Integer, min As Integer
For i = 1 To 500
x = Int(Rnd * 9000) + 1000
Text1 = Text1 & x & vbCrLf
If HuiWenShu(x) Then
Text2 = Text2 & x & vbCrLf
If min = 0 Then
min = x
Else
If min > x Then
min = x
End If
End If
End If
Next i
Text3 = min
End Sub
Function HuiWenShu(ByVal a As Integer) As Boolean
If StrReverse(a) = a Then
HuiWenShu = True
Else
HuiWenShu = False
End If
End Function
Private Sub Command2_Click() Text1 = ""
Text2 = ""
Text3 = ""
End Sub
Private Sub Command3_Click() End
End Sub
6. 级数求和。
Private Sub Command1_Click() Dim s As Double, n As Long Dim m As Double
s = 0
n = 1
Do
m = 1 / (2 * n - 1)
If m < 10 ^ (-5) Then Exit Do
s = s + m
n = n + 1
Loop
Text1 = CStr(s)
End Sub
Private Sub Command2_Click() Text1 = ""
End Sub
Private Sub Command3_Click() End
End Sub
7. 矩阵转置
Option Base 1
Dim a(4, 4) As Integer
Private Sub Command1_Click() Dim b As Integer, c As Integer Dim i As Integer, j As Integer, temp As Integer
Randomize
For b = 1 To 4
For c = 1 To 4
a(b, c) = Int(Rnd * 90) + 10
Text1 = Text1 & a(b, c) & " "
Next c
Text1 = Text1 & vbCrLf Next b
For i = 1 To 4
For j = 1 To i - 1
temp = a(i, j)
a(i, j) = a(j, i)
a(j, i) = temp
Next j
Next i
For i = 1 To 4
For j = 1 To 4
Text2 = Text2 & a(i, j) & " "
Next j
Text2 = Text2 & vbCrLf Next i
End Sub
Private Sub Command2_Click() Text1 = ""
Text2 = ""
End Sub
Private Sub Command3_Click() End
End Sub
8. 靠边元素和
Private Sub Command1_Click() Dim a(1 To 4, 1 To 4) As Integer Dim i As Integer, j As Integer For i = 1 To 4
For j = 1 To 4
a(i, j) = Int(Rnd * 10) + 0
Next
Next
For i = 1 To 4
For j = 1 To 4
Picture1.Print a(i, j);
Next
Picture1.Print
Next
Text1 = fun1(a)
End Sub
Function fun1(x() As Integer) As Double Dim i As Integer, j As Integer fun1 = 0
For i = LBound(x, 1) To UBound(x, 1)
For j = LBound(x, 2) To UBound(x, 2)
If i = LBound(x, 1) Or i = UBound(x, 1) Or j = LBound(x, 2) Or j = UBound(x, 2) Then
fun1 = fun1 + x(i, j)
End If
Next
Next
End Function
Private Sub Command2_Click()
Picture1.Cls
Text1 = ""
End Sub
Private Sub Command3_Click()
End
End Sub
9. 孪生素数
Private Sub Command1_Click()
Dim i As Integer
List1.Clear
For i = 2 To 998
If IsPrime(i) And IsPrime(i + 2) Then
List1.AddItem CStr(i) & " " & CStr(i + 2)
End If
Next
End Sub
Function IsPrime(ByVal n As Integer) As Boolean
Dim i As Integer
For i = 2 To n - 1
If n Mod i = 0 Then Exit Function Next
IsPrime = True
End Function
Private Sub Command2_Click()
List1.Clear
End Sub
Private Sub Command3_Click()
End
End Sub
10.冒泡顺序
Option Base 1
Dim a(10) As Integer
Private Sub Command1_Click() Dim i As Integer, j As Integer, k As Integer, temp As Integer
Randomize
For i = 1 To 10
a(i) = Int(Rnd * 90) + 10
Text1 = Text1 & a(i) & " " Next i
For j = 1 To 9
For k = 1 To 10 - j
If a(k) > a(k + 1) Then
temp = a(k)
a(k) = a(k + 1)
a(k + 1) = temp
End If
Next k
Next j
For j = 1 To 10
Text2 = Text2 & a(j) & " "
Next j
End Sub
Private Sub Command2_Click() Text1 = ""
Text2 = ""
End Sub
Private Sub Command3_Click() End
End Sub
11.逆序数
Private Sub Command1_Click() Dim n As String, s As String Dim i As Integer
n = Text1
For i = Len(n) To 1 Step -1
s = s & Mid(n, i, 1) Next i
Text2 = s
End Sub
Private Sub Command2_Click() Text1 = ""
Text2 = ""
End Sub
Private Sub Command3_Click() End
End Sub
12.判断素数
Private Sub cmdJudge_Click()
Dim x As Integer '待判断的数字
Dim I As Integer '循环变量
x = Val(txtInput.Text)
'根据素数定义进行判断
For I = 2 To x - 1
If x Mod I = 0 Then Exit For
Next I
If I > x - 1 Then
Print CStr(x) & "是素数"
Else
Print CStr(x) & "不是素数"
End If
End Sub
13.水仙花数
Private Sub Command1_Click() Dim i As Integer, a As Integer, b As Integer, c As Integer
For i = 100 To 999
a = i \ 100
b = i \ 10 Mod 10
c = i Mod 10
If i = a * a * a + b * b * b + c * c * c Then
Text1 = Text1 & i & vbCrLf
End If
Next i
End Sub
Private Sub Command2_Click() End
End Sub
14.顺序查找
Dim s(1 To 10) As Integer Private Sub Command1_Click() Dim i As Integer
Text1 = ""
For i = 1 To 10
s(i) = Int(Rnd * 90) + 10
Text1 = Text1 & CStr(s(i)) & " "
Next
End Sub
Private Sub Command2_Click() Dim m As Integer
m = search(s, Val(Text2)) If m > 0 Then
Text3 = "要查找的数是第" & CStr(m) & "个元素" Else
Text3 = "要查找的数不存在"
End If
End Sub
Function search(x() As Integer, ByVal n As Integer) As Integer
Dim i As Integer
search = -1
For i = LBound(x) To UBound(x)
If x(i) = n Then
search = i
Exit Function
End If
Next
End Function
Private Sub Command3_Click() End
15.完全质因子数
Private Sub Command1_Click() Dim i As Integer, j As Integer, s As Integer
List1.Clear
For i = 1 To 100
s = 0
For j = 1 To i - 1
If i Mod j = 0 Then
s = s + j
End If
Next
If s = i Then List1.AddItem i Next
End Sub
Private Sub Command2_Click() List1.Clear
End Sub
Private Sub Command3_Click()
End
End Sub
16.字符统计
Option Base 1
Private Sub Command1_Click() Dim i As Integer, a(26) As Integer, n As Integer
Dim s As String * 1, str1 As String str1 = Text1
n = Len(str1)
For i = 1 To n
s = Mid(str1, i, 1)
If UCase(s) >= "A" And UCase(s) <= "Z" Then
a(Asc(UCase(s)) - 64) = a(Asc(UCase(s)) - 64) + 1
End If
Next i
For i = 1 To 26
If a(i) <> 0 Then
List1.AddItem Chr(64 + i) & ":" & a(i)
End If
Next i
End Sub
Private Sub Command2_Click() Text1 = ""
List1.Clear
End Sub
Private Sub Command3_Click() End
End Sub
17.最大公约数
Private Sub Command1_Click() Dim m As Integer, n As Integer m = Val(Text1)
n = Val(Text2)
Text3 = gcd(m, n)
Text4 = m * n / gcd(m, n) End Sub
Function gcd(ByVal a As Integer, ByVal b As Integer) As Integer
Dim r As Integer
Do
r = a Mod b
a = b
b = r
Loop Until r = 0
gcd = CStr(a)
End Function
Private Sub Command2_Click() Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click() End
18.最大最小平均求和
Dim s(1 To 10) As Integer Private Sub Command1_Click() Dim i As Integer
For i = 1 To 10
s(i) = Int(Rnd * 90) + 10
List1.AddItem s(i)
Next
End Sub
Private Sub Command2_Click() Dim i As Integer
Dim max As Integer, min As Integer, sum As Integer
max = s(1)
min = s(1)
sum = 0
For i = 1 To 10
sum = sum + s(i)
Next
For i = 2 To 10
If s(i) > max Then max = s(i)
If s(i) < min Then min = s(i) Next
Text1 = max
Text2 = min
Text3 = sum / 10
Text4 = sum
End Sub
Private Sub Command3_Click() List1.Clear
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Command1.SetFocus
End Sub
20.最小公倍数
Private Sub Command1_Click() Dim m As Integer, n As Integer m = Text1
n = Text2
Text3 = m * n / gcd(m, n) End Sub
Function gcd(ByVal a As Integer, ByVal b As Integer) As Integer
Dim r As Integer
Do
r = a Mod b
a = b
b = r
Loop Until r = 0
gcd = CStr(a)
End Function
Private Sub Command2_Click() Text1 = ""
Text2 = ""
Text3 = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click() End
End Sub
本文档为【VB编程题】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。