1,下面程序对已知数组a,删除数组中数组元素中某个元素 Private Sub Command1_Click() Dim a(), key%, i%, j%
a = Array(1, 6, 8, 3, 5, 9, 10, 2, 7, 4) key = Val(InputBox(\输入要删除的值\For i = 0 To UBound(a) If key = a(i) Then Forj = i + 1 To UBound(a) a(j - 1) = a(j) Nextj
ReDim Preserve a(UBound(a) - 1) MsgBox (\删除完成\ExitSub End If Next i
MsgBox (\找不到要删除的元素\End Sub
2,下列程序表示将输入的一个数插入到按递减的有序数列中,插入后使该序列仍有序 Private Sub Form_Click() Dim a, i%, n%, m%
a = Array(19, 17, 15, 13, 11, 9, 7, 5, 3,1) n = UBound(a)
ReDim Preserve a(n + 1)
m = Val(InputBox(\输入欲插入的数\For i = UBound(a) - 1 To 0 Step -1 If m >= a(i) Then a(i + 1) = a(i) If i = 0 Then a(i) = m Else a(i + 1) = m Exit For End If Next i
For i = 0 To UBound(a) Print a(i) Next i End Sub
3,下列程序在1000~9999之间查找满足如下条件的整数:该整数,逆向排列得到的 另一个四位数是它自身的倍数(2倍以上)。查找结果和逆向排列数分别显示在对应 的列表框
Private Sub Command1_Click() Dim n As Integer Dim m As Integer Dim i As Integer For i = 1000 To 9999 m= 0 n = i
DoWhile n > 0 m = m * 10 + n Mod 10 n = n \\ 10 Loop
If m Mod i=0And m \\ i > 1 Then list1.AddItem i
list2.AddItem m & \ End If Next i End Sub
4,将100~150之间的偶数,拆分成两个素数之和,最后输出格式如图。其中,prime函数判断
参数x是否为质数
Private Function prime(ByVal x As Integer)As Boolean prime = ture For i = 2 To Sqr(x) If x Mod i = 0 Then prime = False Exit Function End If Next i End Function
Private Sub Form_Load() i = 1
For n = 100 To150 For k = 3 To n / 2
Ifprime(k) And prime(n - k) Then
Print i; \Exit For End If Next k i = i + 1 Next n End Sub 10,
子过程F(N,M,T)对一个四位数n整整数判断:已知该整数n,逆向排列获得另一个4位数m是特自身的倍数,则t为true 表示满足上述条件。。。 Private Sub Command1_Click() Dim t As Boolean, i%, k% text1 = \text2 = \
For i = 1000 To 9999 Call f(i, k, t) Ift Then
text1 = text1 & i & vbCrLf
text2 = text2 & k & \EndIf Next i End Sub
Sub f(ByVal n%, ByRef m%, byreyftag AsBoolean) Dim i% Tag = False m = 0