VB算法特训营(四)

这真的很重要!关于题目版权声明及部分事项 ? 戳它进行查看

最后一期啦,祝福所有访问本站及本页面的考生们加油!?也祝我自己加油!- 2021.3.9

img

今天拿到了2020年的考卷,好他妈简单(自大ing)- 2021.3.4

151.换零钱

img

1
2
3
4
5
6
7
8
9
10
Print "1分", "2分", "5分"
For a = 0 To 10
For b = 0 To 5
For c = 0 To 2
If c * 5 + b * 2 + a * 1 = 10 Then
Print a, b, c
End If
Next
Next
Next

152.输出被整除的数

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Dim a(100) As Integer
For i = 1 To 100
a(i) = Int(Rnd * (999 - 100 + 1)) + 100
Next
For i = 1 To 100
For j = 1 To 100 - i
If a(j) < a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
For i = 1 To 100
If a(i) Mod 3 = 0 Then
Print a(i);
Form1.ForeColor = vbRed
Else
Print a(i);
Form5.ForeColor = vbBlack
End If
If i Mod 5 = 0 Then
Print
End If
Next

154.打印最外层的和

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Print "二维数组为:"
Dim a(5, 5)
For i = 1 To 5
For j = 1 To 5
a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10
Print a(i, j);
Next
Print
Next
Print
Print "最外层的和:"
For i = 1 To 5
For j = 1 To 5
If i = 1 Or j = 1 Or i = 5 Or j = 5 Then
s = s + a(i, j)
If a(5, 5) = a(i, j) Then
Sum = Sum & a(i, j) & "=" & s
Else
Sum = Sum & a(i, j) & "+"
End If
End If
Next
Next
Print Sum

155.杨辉三角

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Dim a(10, 10)
For i = 1 To 10
a(i, i) = 1
a(i, 1) = 1
Next
For i = 1 To 10
For j = 1 To i - 1
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
Next
Next
For i = 1 To 10
For j = 1 To 10
Print a(i, j);
Next
Print
Next

156.动态数组

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Dim a(), l, n, item As Integer
Private Sub Form_Load()
a = Array(5, 15, 23, 52, 68, 88, 90, 99)
End Sub
Private Sub Command1_Click()
Cls
item = Val(InputBox("请输入:"))
n = n + 1
l = UBound(a) + n
ReDim Preserve a(l)
a(l) = item
For i = 1 To UBound(a)
For j = 0 To UBound(a) - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
For i = 0 To UBound(a)
Print a(i);
Next
End Sub

157.计算排名

img

小于400票决,要求对数组进行降序排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Dim a(8, 9)
Me.FontSize = 15
Print "第十四届二中大学全校师生肃反协会第四次会议"
Print " 分数1 分数2 分数3 分数4 分数5 分数6 分数7 总分 是否票决"
For i = 1 To 8
s = 0
For j = 1 To 7
a(i, j) = Int(Rnd * (100 - 1 + 1)) + 1
s = s + a(i, j)
Sum = Sum + s
a(i, 8) = s
Next
Next
For i = 1 To 8
Max = a(i, 8)
For j = i + 1 To 8
If a(j, 8) > Max Then
For t = 1 To 8
Max = a(j, t)
a(j, t) = a(i, t)
a(i, t) = Max
Next
End If
Next
Next
For i = 1 To 8
Print "代表" & i;
For j = 1 To 9
Print Format(a(i, j), "@@@@@@@");
If a(i, 8) < 400 Then
a(i, 9) = "是"
Else
a(i, 9) = "否"
End If
Next
Print
Next
Print "计票总分:" & Sum & " 总分平均率:" & Sum / UBound(a) - LBound(a) - 1

158.三角形面积

img

输入三边长度后,首先检验数据的有效性,如无效则弹出对话框提示不能组成三角形,如有效则进行计算。结果显示在第4个文本框中。求面积公式:$\sqrt{p(p-a)(p-b)(p-c)}$其中$p=(a+b+c)/2$,a、b、c表示三角形三条边的值。单击清除按钮则清空各文本框中的内容,同时使文本框1获得焦点。文本框4被锁定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Private Sub Command1_Click()
Dim a, b, c, p, sum As Double
a = CDbl(Text1.Text)
b = CDbl(Text2.Text)
c = CDbl(Text3.Text)
If a + b > c Or a + c > b Or c + b > a Then
p = (a + b + c) / 2
pa = p - a
pb = p - b
pc = p - c
sum = Round(Sqr(p * (pa * pb * pc)), 5)
Text4.Text = sum
Text4.Enabled = True
Else
MsgBox "不能组成三角形!"
End If
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
Text4.Enabled = False
sum = 0
End Sub

159.倒计时

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Dim i, n As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Label1 = n \ 60 Mod 60 & "分" & n Mod 60 & "秒"
End Sub
Private Sub Option1_Click(Index As Integer)
Select Case Index
Case 0
n = 60
Case 1
n = 300
Case 2
n = 600
End Select
End Sub
Private Sub Timer1_Timer()
Cls
If n = 0 Then
MsgBox "时间到!"
Else
n = n - 1
Label1 = n \ 60 Mod 60 & "分" & n Mod 60 & "秒"
End If
End Sub

160.同构数

img

1
2
3
4
5
6
7
8
9
10
11
Dim n, a, b, c, d, e, f As Integer
For i = 1 To 1000
n = i ^ 2
a = n Mod 10
b = n Mod 100
c = n Mod 1000
d = n Mod 10000
If i = a Or i = b Or i = c Or i = d Then
Print i, n
End If
Next

参考答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
s = 0
For i = 1 To 1000
res = i ^ 2
t = 10
Do While res \ t > 0
j = res Mod t
t = t * 10
Print j, t
If j = i Then
Exit Do
End If
Loop
If j = i Then
Print i
End If
Next

161.字体修改

img

嫌麻烦的可以直接拷代码,完全没问题的,我栈区空间还蛮大的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Private Sub Combo5_Click()
If Combo5.ListIndex = 0 Then Text1.Alignment = 0
If Combo5.ListIndex = 1 Then Text1.Alignment = 1
If Combo5.ListIndex = 2 Then Text1.Alignment = 2
End Sub
Private Sub Combo6_Click()
If Combo6.ListIndex = 0 Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
End If
If Combo6.ListIndex = 1 Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
If Combo6.ListIndex = 2 Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
End Sub
Private Sub Combo7_Click()
For i = 0 To 6
If Combo7.ListIndex = i Then Text1.FontSize = i * 10 + 10
Next
End Sub
Private Sub Combo8_Click()
If Combo8.ListIndex = 0 Then Text1.Font = "黑体"
If Combo8.ListIndex = 1 Then Text1.Font = "宋体"
If Combo8.ListIndex = 2 Then Text1.Font = "仿宋"
If Combo8.ListIndex = 3 Then Text1.Font = "微软雅黑"
End Sub
Private Sub Combo1_Click()
If Combo1.ListIndex = 0 Then Text1.ForeColor = vbBlack
If Combo1.ListIndex = 1 Then Text1.ForeColor = vbWhite
If Combo1.ListIndex = 2 Then Text1.ForeColor = vbRed
If Combo1.ListIndex = 3 Then Text1.ForeColor = vbYellow
If Combo1.ListIndex = 4 Then Text1.ForeColor = vbBlue
End Sub
Private Sub Combo2_Click()
If Combo2.ListIndex = 0 Then Text1.BackColor = vbBlack
If Combo2.ListIndex = 1 Then Text1.BackColor = vbWhite
If Combo2.ListIndex = 2 Then Text1.BackColor = vbRed
If Combo2.ListIndex = 3 Then Text1.BackColor = vbYellow
If Combo2.ListIndex = 4 Then Text1.BackColor = vbBlue
End Sub
Private Sub Form_Load()
Combo5.AddItem "左对齐": Combo5.AddItem "右对齐": Combo5.AddItem "居中"
Combo6.AddItem "斜体": Combo6.AddItem "粗体": Combo6.AddItem "下划线"
Combo7.AddItem "10": Combo7.AddItem "20": Combo7.AddItem "30"
Combo7.AddItem "40": Combo7.AddItem "50": Combo7.AddItem "60"
Combo7.AddItem "70": Combo8.AddItem "黑体": Combo8.AddItem "宋体"
Combo8.AddItem "仿宋": Combo8.AddItem "微软雅黑": Combo1.AddItem "黑色"
Combo1.AddItem "白色": Combo1.AddItem "红色": Combo1.AddItem "黄色"
Combo1.AddItem "蓝色": Combo2.AddItem "黑色": Combo2.AddItem "白色"
Combo2.AddItem "红色": Combo2.AddItem "黄色": Combo2.AddItem "蓝色"
End Sub

162.删除数组元素

img

打开考生文件夹内 vb4 文件夹下的“工程 1.vbp”文件进行设计,界面如图所示。某数组有10个元素,其值为随机产生的两位整数。从键盘上输入一个数,若这个数在数组当中,就把它删除,并输出删除该数后的数组;若这个数不在数组中,提示“该数不在数组中”。 注意:如果数组中存在多个与该数相同的元素,每一个都能删除;能实现多次删除。
项目文件保存在文件夹vb4内。(20分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Dim a(), n, key, item As Integer, flag As Boolean
Private Sub Form_Click()
Cls
ReDim a(0 To 9)
Print "原来数组为:"
For i = 0 To UBound(a)
a(i) = Int(Rnd * (99 - 10 + 1)) + 10
Print a(i);
Next
Print
End Sub
Private Sub Command1_Click()
item = Val(InputBox("查找的数是:"))
flag = False
For i = 0 To UBound(a)
If item = a(i) Then
key = i
flag = True
End If
Next
If flag = True Then
For i = key To UBound(a) - 1
a(i) = a(i + 1)
Next
Print: n = n + 1
Print "删除" & item & "后的数组是:"
For i = 0 To UBound(a) - n
Print a(i);
Next
Else
Print
Print item & "不在该数组中"
End If
End Sub

163.计时器

打开考生文件夹内 vb2 文件夹下的“工程 1.vbp”文件进行设计,界面如图所示。要求:(1)标签Label1用来显示累计的时间,Caption初值设置为0,Autosize设置为True,字体、字号、颜色等属性自定。(2)定时器Timer1的Interval设置为1秒,Enabled初值设为False。(3)命令按钮Command1的标题设置为“计时开始”,程序运行后单击“计时开始”按钮,计时器开始计时并且按钮标题变为“停止”;单击“停止”按钮,则停止计时,同时消息框显示一共运行几小时几分几秒。
项目文件保存在文件夹vb2内。 (15分)

img img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Dim i, m, h, s, flag As Integer
Private Sub Command1_Click()
flag = flag + 1
i = 0
If flag Mod 2 = 0 Then
Command1.Caption = "开始计时"
Timer1.Enabled = False
h = Val(Label1) \ 3600
m = (Val(Label1) Mod 3600) \ 60
s = Val(Label1) Mod 60
MsgBox "一共运行了" & h & "小时" & m & "分" & s & "秒"
Label1 = 0
Else
Command1.Caption = "停止"
Timer1.Enabled = True
Label1 = 0
End If
End Sub
Private Sub Timer1_Timer()
i = i + 1
Label1 = i
End Sub

164.判断回文

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Dim flag As Boolean
a = Text1
flag = False
For i = 1 To Len(a) / 2
res = Mid(a, 1, Len(a) \ 2)
ser = StrReverse(Right(a, Len(a) \ 2))
If Mid(res, i, 1) = Mid(ser, i, 1) Then
flag = True
Else
flag = False
Exit For
End If
Next
If flag = True Then
Print a & "是回文"
Else
Print a & "不是回文"
End If

165.换零钱

img

1
2
3
4
5
6
7
8
9
10
a = 0: b = 1
For i = 1 To 30
s = a + b
b = a
If s > 1000 Then Exit For
a = s
sum = sum + s
Print s
Next
Print "数列和:" & sum

166.矩阵相加

img

注:列表框换为文本框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Text1 = "": Text2 = "": Text3 = ""
Dim a%(1 To 20), b!(1 To 20), sum!(1 To 20)
For i = 1 To 20
a(i) = Int(Rnd * (10 - 0 + 1)) + 0
If i Mod 5 <> 0 Then
Text1 = Text1 & a(i) & " "
Else
Text1 = Text1 & vbCrLf
End If
Next
For i = 1 To 20
b(i) = Int(Rnd * (10 - 0 + 1)) + 0
If i Mod 5 <> 0 Then
Text2 = Text2 & b(i) & " "
Else
Text2 = Text2 & vbCrLf
End If
Next
For i = 1 To 20
sum(i) = a(i) + b(i)
If i Mod 5 <> 0 Then
Text3 = Text3 & sum(i) & " "
Else
Text3 = Text3 & vbCrLf
End If
Next

167.选择排序

选择排序(Selection sort)的工作原理是:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Dim a(1 To 10)
For i = 1 To 10
a(i) = Int(Rnd * (99 - 10 + 1)) + 10
Print a(i);
Next
For i = 1 To 10
Key = i
For j = i + 1 To 10
If a(j) < a(Key) Then
Key = j
End If
Next
If Key <> i Then
t = a(i)
a(i) = a(Key)
a(Key) = t
End If
Next: Print
For i = 1 To 10
Print a(i);
Next

168.字符串加密

思想:将要加密的每个字母加上或减去一个序数,例:加上序数5,这时”A”->”F”,”a”->”f”,”Z”->”E”,以此类推

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Text2 = "": Text3 = ""
a = Text1
n = 5
For i = 1 To Len(a)
res = Mid(a, i, 1)
If res >= "a" And res <= "z" Then
t = Asc(res) + n
If t > Asc("z") Then
t = t - 26
End If
ElseIf res >= "A" And res <= "Z" Then
t = Asc(res) + n
If t > Asc("Z") Then
t = t - 26
End If
End If
Text2 = Text2 & Chr(t)
Next
a = Text2
For i = 1 To Len(a)
res = Mid(a, i, 1)
If res >= "a" And res <= "z" Then
t = Asc(res) - n
If t > Asc("z") Then
t = t + 26
End If
ElseIf res >= "A" And res <= "Z" Then
t = Asc(res) + n
If t > Asc("Z") Then
t = t + 26
End If
End If
Text3 = Text3 & Chr(t)
Next

169.字符串处理

说明:统计英文单词的个数,如果以“ ”,“?”,“!”,“.”,“,”,“;”结尾视为一个单词

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Dim flag As Boolean, n As Integer, res As String
flag = False
For i = 1 To Len(Text1)
res = Mid(Text1, i, 1)
If res = " " Or res = "?" Or res = "!" Or res = "." Or res = "," Or res = ";" Then
flag = False
Else
If flag = False Then
n = n + 1
flag = True
End If
End If
Next
Print "单词数量为:" & n

170.角谷猜想

给一个自然数,如果为偶数则除以2,如果为奇数则乘以3加1,得到一个新的数字后继续演算,若干次后结果必然为1

img

1
2
3
4
5
6
7
8
9
10
11
12
13
Dim key As Integer
key = 100
Do While key <> 1
If key Mod 2 = 0 Then
Print key & "/2=" & key / 2
key = key / 2
Else
Print Trim(key);
key = key * 3 + 1
Print "*3+1=" & Trim(key);
Print
End If
Loop

171.四方定律

说明:四方定律即自然数最多只要用4个数的平方和就可以求出

img

1
2
3
4
5
6
7
8
9
10
11
12
13
t = 2333
For a = 1 To t
For b = 1 To a
For c = 1 To b
For d = 1 To c
If a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2 = t Then
Print a & "^2+"; b & "^2+" & c & "^2+" & d & "^2=" & t
Exit Sub
End If
Next
Next
Next
Next

172.判断质数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Dim flag As Boolean
n = 3
flag = True
For i = 1 To n - 1
s = 0
If i Mod 2 = 0 Then
s = s + 1
End If
If s > 0 Then
flag = True
Else
flag = False
End If
Print s
Next
If flag Then
Print "是质数"
Else
Print "不是质数"
End If

173.求数组最大值

img

如图求出数组每行的最大值,并标出它的坐标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Dim a(5, 5)
For i = 1 To 5
Max = a(1, 1)
For j = 1 To 5
a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10
If a(i, j) > Max Then
Max = a(i, j)
Key = j
x = i
y = j
End If
Print a(i, j);
Next
Print "Max: " & a(i, Key) & " a(" & x & "," & y & ")";
Print
Next

174.打印矩阵

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Dim a(5, 5) As Integer
Dim str As String
Private Sub Form_Click()
Cls
For i = 1 To 5: n = n + 1: a(i, 1) = n: Next
For i = 2 To 5: n = n + 1: a(5, i) = n: Next
For i = 4 To 1 Step -1: n = n + 1: a(i, 5) = n: Next
For i = 4 To 2 Step -1: n = n + 1: a(1, i) = n: Next
For i = 2 To 4: n = n + 1: a(i, 2) = n: Next
For i = 3 To 4: n = n + 1: a(4, i) = n: Next
For i = 3 To 2 Step -1: n = n + 1: a(i, 4) = n: Next
For i = 2 To 3: n = n + 1: a(i, 3) = n: Next
For i = 1 To 5
For j = 1 To 5
Print Format(a(i, j), "@@@@");
Next
Print
Next
End Sub

175.快速排序

img

  1. 从数列中挑出一个元素,称为 “基准”(pivot);
  2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;
  3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序;

如图编程实现随机生成11个两位数并进行排序

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Private Sub Form_Click()
Cls
Dim a(0 To 10)
For i = 0 To 10
a(i) = Int(Rnd * (99 - 10 + 1)) + 10
Print a(i);
Next
Print
QuickSort a(), LBound(a), UBound(a)
Print
For i = 0 To 10
Print a(i);
Next
End Sub
Sub QuickSort(MyArray(), L, R) '获取数组,并取得下、上界值到L、R
Dim i, j, pivot, y
i = L '确定从数组左边与轴比较的元素位置I
j = R '确定从数组右边与轴比较的元素位置J
pivot = MyArray((L + R) / 2) '将X选取为数组的轴
While (i <= j) '当左右查询位置没有相交时执行
While (MyArray(i) < pivot And i < R) '在左边找出大于 X 的值
i = i + 1
Wend
While (pivot < MyArray(j) And j > L) '在右边找出小于 X 的值
j = j - 1
Wend
If (i <= j) Then '当查询的位置没有相交时执行
y = MyArray(i) '将小于轴的元素放在左边,大于轴的换到右边
MyArray(i) = MyArray(j)
MyArray(j) = y
i = i + 1 '移动从左边开始的查询位置
j = j - 1 '移动从右边开始的查询位置
End If
For t = 0 To 10
Print MyArray(t);
Next
Print
Wend
'递归过程
If (L < j) Then Call QuickSort(MyArray(), L, j) '分析轴左边的数组
If (i < R) Then Call QuickSort(MyArray(), i, R) '分析轴右边的数组
End Sub

代码来源:从VB来看-QuickSort(VB快速排序)_呓语-CSDN博客_vb 快速排序

176.验证哥德巴赫猜想

img

每个不小于6的偶数都可以表示为两个奇素数之和;2.每个不小于9的奇数都可以表示为三个奇素数之和.考虑把偶数表示为两数之和,而每一个数又是若干素数之积.把命题”每一个大偶数可以表示成为一个素因子个数不超过a个的数与另一个素因子不超过b个的数之和”记作”a+b”,那么哥氏猜想就是要证明”1+1”成立.1966年陈景润证明了”1+2”成立,即”任何一个大偶数都可表示成一个素数与另一个素因子不超过2个的数之和”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Private Sub Form_Click()
Cls
Dim key%
key = 50
Print "验证哥德巴赫猜想" & key & "以内的真偶数是成立的"
For i = 2 To 50 Step 2
For j = 2 To key \ 2
t = key - j
t1 = prime(j)
If t1 = True Then
t2 = prime(t)
If t2 = True Then
Print Format(i & "=" & t & "+" & j, "@@@@@@@@@@@");
n = n + 1
If n Mod 15 = 0 Then
Print
End If
End If
End If
Next
Next
End Sub
Private Function prime(n) As Boolean
prime = True
For i = 2 To n - 1
If n Mod i = 0 Then
prime = False
Else
prime = True
End If
Next
End Function

177.数字金字塔

打印金字塔

img

1
2
3
4
5
6
7
8
9
10
For i = 1 To 9
Print Space(30 - 3 * i);
For j = 1 To i
Print j;
Next
For j = i - 1 To 1 Step -1
Print j;
Next
Print
Next

178.交换数组

编写程序,单击窗体,用随机函数产生4 行5 列的两位整数并输出到屏幕中。实现将第1 行和第3 行数据交换或第2 列和第4 列数据交换,并再次输出到窗体。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Dim a(4, 5): Cls
For i = 1 To 4
For j = 1 To 5
a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10
Print a(i, j);
Next
Print
Next: Print: Print
For i = 1 To 5
t = a(1, i)
a(1, i) = a(3, i)
a(3, i) = t
Next
For i = 1 To 4
t = a(i, 2)
a(i, 2) = a(i, 4)
a(i, 4) = t
Next
For i = 1 To 4
For j = 1 To 5
Print a(i, j);
Next
Print
Next

179.只出现一次的数字

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

例子:手头有 $[1,2,3,3,2]$ 这五个数,只出现一次的数字是1

1
2
3
4
5
6
7
Dim a(5)
a(1) = 2: a(2) = 2: a(3) = 3: a(4) = 3: a(5) = 2: a(5) = 9
b = 0
For i = 1 To UBound(a)
b = b Xor a(i)
Next
Print b

180.综合排序查找

现在需要你生成随机生成1000-1范围内的100的数字,要求你使用一个稳定的排序算法进行排序,然后使用二分查找法进行查询该数组中的任意一个数字

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Dim a(1 To 100) As Integer
Private Sub Command1_Click()
Cls
Print "排序前:"
For i = 1 To 100
a(i) = Int(Rnd * (1000 - 1 + 1)) + 1
Print a(i);
If i Mod 10 = 0 Then Print
Next
For i = 1 To 100
For j = 1 To 100 - i
If a(j) > a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = t
Next
Next
Print "排序后:"
For i = 1 To 100
Print i & ":" & Format(a(i), "@@@") & Space(2);
If i Mod 10 = 0 Then Print
Next
End Sub
Private Sub Command2_Click()
Print
Dim search, mid, guess As Integer
Dim low, high As Integer
Static n As Integer
n = n + 1
search = Val(InputBox("请输入要查询的数字:", "第" & n & "次"))
low = 0
high = UBound(a) - 1
Do While low <= high
mid = (low + high)
guess = a(mid)
If guess = search Then
Print search & "在" & mid & "处"
Exit Sub
End If
If guess > search Then
high = mid - 1
Else
low = mid + 1
End If
Loop
End Sub

181.百钱百鸡

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
For a = 0 To 100
For b = 0 To 100
For c = 0 To 100
If (5 * a + (3 * b) + c / 3) = 100 And _
c Mod 3 = 0 And _
a + b + c = 100 Then
n = n + 1
Print "#" & n & " : VB : " & a _
& " CPT : " & b & " : " _
& " SER : " & c
End If
Next
Next
Next
' 二种
For a = 0 To 33
For b = 0 To 20
c = 100 - a - b
If 5 * b + 3 * a + c / 3 = 100 Then
n = n + 1
Print "#" & n & " : VB : " & b _
& " CPT : " & a _
& " : " & " SER : " & c
End If
Next
Next

182.简单计算题

img

1
2
3
4
5
6
7
8
9
Dim f, s As Double
n = 1
num = 10
For i = 1 To num - 1
f = f * -(1 ^ 2) + (1 / (i + 3))
Next
s = 1 - f
Print s
Print Int(s * 1000) / 1000

183.格利高里公式

img

1
2
3
4
5
6
7
s = 0
a = 1
For i = 1 To 99 Step 2
s = s + 1 / i * a
a = -a
Next
Print Round(s * 4, 2)

184.趣味数学题

img

185.打印题

1
2
3
4
5
6
7
8
9
10
n = 20
Form1.FontSize = 25
For i = 1 To n
If i = 1 Then Print Space(n - 1) & "*";
If i > 1 And i <= n - 1 Then
Print Space(n - i) & "*" & Space(i - 2) & "*" & Space(i - 2) & "*";
End If
If i = n Then Print String(i * 2 - 1, "*");
Print
Next

190.四则运算

img

加减乘除啦,自己理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Private Sub Command1_Click()
Select Case Combo1.ListIndex
Case 0
Text3 = Val(Text1) + Val(Text2)
Case 1
Text3 = Val(Text1) - Val(Text2)
Case 2
Text3 = Val(Text1) * Val(Text2)
Case 3
If Val(Text2) = 0 Then
MsgBox "0不能作为除数!", vbExclamation, "错误"
Exit Sub
Else
Text3 = Val(Text1) / Val(Text2)
End If
End Select
End Sub
Private Sub Form_Load()
Combo1.AddItem "+"
Combo1.AddItem "-"
Combo1.AddItem "*"
Combo1.AddItem "/"
Combo1.Text = Combo1.List(0)
End Sub

191.整数对换

img

1
2
3
4
5
6
n = 56
For i = 10 To 99
If n + i = Val(StrReverse(n)) + Val(StrReverse(i)) Then
Print n & "+(" & i; ")=(" & Val(StrReverse(i)) & ")+" & Val(StrReverse(n))
End If
Next

192.打印

img

1
2
3
4
5
6
7
For i = 0 To 4
For j = 0 To i
t = i * 5 + j
Print t;
Next
Print
Next

193.打印幻方阵

img

结语

2021年4月22日补充:鬼知道VB考得那么简单,人傻了

从2020年12月份开始为技能考做准备,临时补的VB,感谢这期间以来访问本站的游客。这些VB题目都来自互联网,答案有自己写的也有别人的(Sansmall),但还是自己写的比较多。搬运别人代码的下边会有出处,但省略了一些有用的东西。

我主要用Windows-XP来做VB,原因是XP兼容性比较好,加上初中用XP写VB所以用的比较习惯,现在Win10和Win7听说要开兼容性和禁用桌面元素才可以使用,否则会一直闪。

How To Install Visual Basic 6 (VB6) in Windows 10 • Raymond.CC

Visual Basic 6 SP6 Working in Windows 10 64-bit - YouTube

VB刷题到这就告一段落了,接下来要搞别的东西了,有缘再见哈(VB

Author

IceCliffs

Posted on

2021-03-08

Updated on

2024-12-03

Licensed under

Comments