实现上述功能的VB程序如下,但加框处语句有错,请改正。
Const n1=9 ‘第1段已排序数据长度为n1
Const n2=6 ‘第2段已排序数据长度为n2
‘数组c长度为n1+n2,依次存储第1,2段数据
Dim c(1 To n1+n2)As Integer
Private Sub Command1_Click()
‘数组c依次存储两段已按从小到大排序的数据
‘并在列表框List1中显示,代码略
List2.Clear
A位置
For i=n1+1 To n1+n2
j=i
B位置
Do While j>1 And Flag
t=c(j)
c(j)=c(j-1)
c(j-1)=t
C位置
Else
Flag=False
End If
j=j-1
‘②
Loop
Next i
For i=1 To n1+n2
List2.Addltem c(i)
Next i
End Sub
i = 1 : k = 0
Do While i<= 3
For j = 8 To i + 1 Step -1
If a(j) >= a(j - 1) Then
t = a(j): a(j) = a(j-1): a(j-1) = t
k = k + 1
End If
Next j
i = i + 1
Loop
若数组a中的数据依次为“24,29,31,24,12,10,12,25”,则加工结束后,变量k的值为( )
实现上述功能的VB程序如下,但加框处代码有错,请改正。
Const n = 10
Dim a(1 To n) As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer, t As Integer, bottom As Integer
'获取排序前数据依次存储在数组a 中,并在文本框Text1 中显示。代码略
bottom = n : i = 1
Do While i <= bottom - 1
For j = bottom To i + 1 Step -1
If
Then ‘⑴
t = a(j): a(j) = a(j - 1): a(j - 1) = t
ElseIf a(j) = a(j - 1) Then ' 相邻两个数据相等,进行剔除处理
‘⑵
bottom = bottom - 1
End If
Next j
i = i + 1
Loop
Text2.Text = " "
For i = 1 To bottom
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
⑴ ⑵
以下程序加框处有错,请改正。
Private Sub Command1_Click()
Dim s(1 To 1000)As Single, n As Integer, x As Integer
Dim payment As Single, cost As Single, tmp as Single
‘假设顾客共挑选了n本书,各本书金额数都已存放在数组s中
‘先对n本书按价格降序排序
For i=1 To n-1
x=i
For j=i+1 To n
If
Then x=j ’
Next j
If x <> i Then
tmp=s(x)
s(x)=s(i)
s(i)=tmp
End If
Next i
‘降序排序后,每隔两本就是一本可以减免的书
cost=0 : payment=0
For i=1 To n
cost=cost+s(i)
If
Then ‘
payment=payment+s(i)
End If
Next i
Label1.Caption=“总金额:” & cost
Label2.Caption=“实付:” & payment
End Sub
|
排序前 |
86 |
71 |
5 |
41 |
81 |
79 |
37 |
89 |
|
排序后 |
5 |
37 |
41 |
71 |
79 |
89 |
86 |
81 |
实现上述功能的VB程序如下,但加框处代码有错,请改正。
Const n = 8
Dim a(1 To n) As Integer
Private Sub Command1_ click ( )
Dim i As Integer, j As Integer, k As Integer, t As Integer
Dim flag As Boolean, sl As String, s2 As String
‘ 读取一组正整数,存储在数组a中
‘ 代码略
For i = 1 To n
s1 = s1 + Str(a(i))
Next i
Text1. Text = s1
For i = 1 To n-1
‘①
If IsPrime(a(k)) Then flag = True
Else flag = False
For j = i + 1 To n
If IsPrime (a(j)) Then
If
Then ‘②
k = j
flag = True
End If
End If
Next j
If k <> i Then
t = a(k):a(k) = a(i):a(i) = t
End If
If Not flag Then Exit For ‘ Exit For表示退出循环
Next i
‘依次输出排序后的数据
‘代码略
For i=1 To n
s2 = s2 + Str(a(i))
Next i
Text2. Text = s2
End Sub
Function IsPrime (m As Integer) As Boolean
‘ 本函数判断整数m是不是素数。是素数返回值为True,不是素数返回值为False
‘ 代码略
End Function
加框①处代码应改为。
加框②处代码应改为。
For i=1 To 3
For j=1 To 5-i
If a(j)>a(j+1) Then
t=a(j): a(j)=a(j+1): a(j+1)=t
End If
Next j
Text1.Text = Text1.Text + Str(a(i))
Next i
数组元素a(1)到a(5)的值依次为“3,9,6,8,4”。该程序段执行后,文本框 Text1 显示的内容是( )
|
排序前 |
86 |
71 |
5 |
41 |
81 |
79 |
37 |
89 |
|
排序后 |
5 |
37 |
41 |
71 |
79 |
89 |
86 |
81 |
实现上述功能的VB代码如下,但加框处有错,请改正。
Const n=8
Dim a(1 To n)As Integer
Private Sub Commandl_Click()
Dim i As Integer,j As Integer,k As Integer,t As Integer
Dim flag As Boolean
'读取一组正整数,存储在数组a中,代码略
For i= 1 To n-1
'(1)
If IsPrime(a(k))Then flag = True Else flag = False
For j = i + 1 To n
If IsPrime(a(j)) Then
If
Then '(2)
k=j
flag = True
End If
End If
Next j
If k <> i Then
t=a(k):a(k)= a(i):a(i)= t
End If
If Not flag Then Exit For 'Exit For表示退出循环
Next i
'依次输出排序后的数据。代码略
End Sub
Function IsPrime(m As Integer)As Boolean
'本函数判断m是不是素数:是素数返回值为True,不是素数返回值为False
'代码略
End Function
⑴ ⑵
|
学生编号 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
|
买菜时间 |
3 |
2 |
5 |
5 |
6 |
7 |
1 |
8 |
1 |
3 |
|
最终是否参与排队 |
是 |
是 |
是 |
是 |
否 |
是 |
是 |
否 |
是 |
是 |
Const n = 10
Dim a(1 To n)As Integer
Private Sub Command1_Click()
Dim i As Integer,s As Integer,ans As Integer,t As Integer
'读取每个学生的买菜时间,依次存储在数组a中,代码略
For i = 1 To n-1
k = i
For j = n To i + 1 Step - 1
If Then k = j
Next j
If a(i)<>a(k)Then t=a(i):a(i)= a(k):a(k)=t
Next i
s = 0:ans = 0
For i = 1 To n
If
Then s=s + a(i):ans = ans + 1
Next i
Label1.Caption ="共有"+ Str( ans)+"个人是愉快的,且参与排队买菜。"
按上述要求,编写一个VB程序,功能如下:在列表框List1中显示整理前的所有物资相关数据,单击“库存合并统计”按钮Command1,在列表框List2中显示整理结果,程序运行界面如图所示。

Const n= 2000 ‘物资总项数
Const nc = 26 ‘物资种别数,最多不会超过26
Dim items(1 To n) As String, supplies(1 To n) As String, totals(1 To n) As Long
Dim g(1 To2 * nc) As Long ‘存储各种物资3天、7天的需求数量
Dim b(1 To nc) As Long ‘存储各种物资的库存量
Dim c(1 To nc) As Long
Dim q(1 To nc) As Integer
Private Sub Form_Load()
‘本过程读取编号、品名、收支、需求的数据分别存储在数组items、supplies、totals、g数组中,并在List1中显示有关数据:g数组中g(1)、g(2)存储物资A的3天、7天的需求数量;g(3)、g(4)存储物资B的3天、7天的需求数……代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, top As Integer, bottom As Integer
Dim r As Long, s As String, t As Integer, m As Integer
For i=1 To nc.
b(i)=0:c(i)=0
Next i
For i=1 To n ‘统计每种物资的库存量
k = Asc(Mid(items(i), 2, 1))- Asc("A")+1
①
c(k)=i
Next i
j=0
For i=1 To nc
If c(i)<>0 Then j=j+1: q(j)=i
Next i
top = 0: bottom=j+ 1
j=1: k=bottom-1
Do While j<bottom ‘按各种物资的需求量进行等级分类
m=q(j)
②
If r<g(2*m-1) Then
top=top+1
If top<>j Then t= q(top): q(top)=q(j): q(j)=t
j=j+1
ElseIf r>= g(2*m) Then
bottom=bottom-1
t= q(j): q(j)=q(bottom): q(bottom)=t
Else
j=j+1
End If
Loop
List2.AddItem "编号 品名 库存"
List2. AddItem "------------------------------"
For i= 1 To k
s= fp(i, top, bottom)
List2.AddItem" " & s & " " & supplies(c(q(i))) & " " & b(q(i))
Next i
End Sub
Function fp(pos As Integer, head As Integer, tail As Integer) As String
If ③ Then
fp= "A"
ElseIf pos < tail Then
fp= "B"
Else
fp="C"
End If
fp = fp & Mid(items(c(q(pos))), 2, 1)
End Function
① ② ③
For i = 1 To 2
For j = i + 1 To 6
If a(i) > a(j) Then
t = a(j): a(j) = a(i): a(i) = t
End If
Next j
Next i
数组元素 a(1)到 a(6)的值依次为“3,4,2,1,5,0”,经过该程序段“加工”后,数组元素 a(1)到 a(6)的值依次为( )。
实现上述功能的VB程序如下,在橫线处填入合适的代码。
Const n = 10
Dim a(1 To n) As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer, t As Integer
Dim k As Integer, bottom As Integer
'获取排序前数据,依次存储在数组 a 中,并在文本框 Text1 中显示。代码略
bottom = n: i = 1
Do While i <= bottom - 1
k = i: j = bottom
Do While j > i
If a(j) < a(k) Then
k = j
ElseIf a(j) = a(i) Then '若发现重复数据,进行剔除处理
a(j) = a(bottom)
IfThen k = j
bottom = bottom - 1
End If
j = j - 1
Loop
If k <> i Then
t = a(k): a(k) = a(i): a(i) = t
Else
EndIF
Loop
For i = 1 To
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
n = 8
L = 1: R = n
For i = 1 To n - 1
If i Mod 2 = 1 Then
pos = L: L = L + 1
Else
pos = R: R = R - 1
End If
k = pos
For j = L To R
If a(j) < a(k) Then k = j
Next j
If pos <> k Then
t = a(pos): a(pos) = a(k): a(k) = t
End If
Next i
已知a数组的各元素值分别为12,18,23,25,9,47,29,36,则运行程序段后,a数组各元素的值变为 ( )
For i=1 To 2
For j=2 To7-i
If a(j-1)>a(j)Then
t=a(j):a(j)=a(j-1):a(j-1)=t
End If
Next j
Next i
若数组元素a(1)到a(6)的值依次为“8,2,9,3,5,1”,则经过该程序段“加工”后,数组元素a(1)到a(6)的值依次为( )

控件属性设置如下:
|
对象 |
属性 |
属性值 |
说明 |
|
Form1 |
Caption |
幸运的猪 |
窗体的标题 |
|
Label1 |
Caption |
当天要杀猪的数量 |
标签显示 |
|
Label2 |
Caption |
幸运猪的原始编号 |
标签显示的文字 |
|
Text1 |
Text |
空白 |
文本框的文本 |
|
Text2 |
Text |
空白 |
文本框的文本 |
|
Image1 |
Picture |
Pig.gif |
显示的图片 |
|
Command1 |
Caption |
计算 |
按钮显示的文字 |
Private Sub Command1_Click()
Dim sum, i, pos, last, count As Integer
Dim a(1 To 100) As Integer
For i = 1 To sum
a(i) = 1 '初始状态为1表示还没被杀掉
Next i
count=0
Do while '要杀掉sum-1头猪
Pos=0
For i = 1 To sum
If a(i) <>0 Then
pos = pos + 1 'pos表示报数
If Then '报到奇数的猪被屠宰
count = count + 1
a(i) = 0
End If
End If
Next i
Loop
For i = 1 To sum
If a(i) = 1 Then
Text2.Text = Str(i)
End If
Next i
End Sub
Dim i As Integer, imax As Integer
①
For i = 3 To 10
If Abs(a(i) - a(i - 1)) >= ② Then imax = i
Next i
Text1.Text = "GDP变化最大的年份区间是" + Str(imax + 2008) + "-" + Str(imax + 2009)
划线处①、②应填入的代码为( )
Private Sub Command1_Click()
Const n = 100
Dim ss As String, t As String, i As Integer
Dim k As Integer, p As Integer, temp As Integer
Dim s(1 To n) As Integer, f(1 To n) As Integer
Dim num(1 To n) As Integer, flag(1 To n) As Boolean
ss = Text1.Text
starttime = 480: endtime = 720 '教室可以安排活动时间为8:00到12:00
k = 0: p = 0
For i = 1 To Len(ss)
ch = Mid(ss, i, 1)
If ch <> "," Then
t = t + ch
Else
p = (k - 1) \ 2 + 1
If k Mod 2 = 1 Then s(p) = convert(t) Else f(p) = convert(t)
t = ""
End If
Next i
For i = 1 To p
num(i) = i
Next i
For i = 1 To p
List1.AddItem Str(num(i)) + " " + ff(s(i)) + " " + ff(f(i))
Next i
For i = 1 To p - 1
For j = p To i + 1 Step -1
If
Then
temp = num(j): num(j) = num(j - 1): num(j - 1) = temp
temp = s(j): s(j) = s(j - 1): s(j - 1) = temp
temp = f(j): f(j) = f(j - 1): f(j - 1) = temp
End If
Next j
Next i
For i = 1 To p
If s(i) >= starttime And f(i) <= endtime Then
flag(i) = True
End If
Next i
List2.AddItem "活动号 起始时间 结束时间"
For i = 1 To p
IfThen
List2.AddItem Str(num(i)) + " " + ff(s(i)) + " " + ff(f(i))
End If
Next i
End Sub
Function convert(s As String) As Integer
'把时间格式的s转换为整数,如"08:20"转化为500。代码略
End Function
Function ff(t As Integer) As String
'把整数t转换为时间格式,如500转化为"08:20"。代码略
End Function
For i=1 To 6
k=i
For j=i+1 To 7
If a(j)<a(k) Then k=j
Next j
If i<>k then
t=a(i) : a(i)=a(k) : a(k)=t
End If
Next i
在排序过程中,经过某一遍排序“加工”后,数组元素a(1)到a(7)的数据依次为“10,41,75,12,63,11,85”,则下一遍排序“加工”后数组元素a(1)到a(7)的数据依次是( )
按上述要求,编写一个VB程序,功能如下:在列表框List 1中显示整理前的数据,单击“整理”按钮Command 1,整理结果显示在列表框List 2中,程序运行界面如图所示。
Const n=200 ‘报名总人数
Const nc=10 ‘城市数
Dim city(1 To n) As Integer, pname(1 To n) As String, times(1 To n) As Integer
Dim b(1 To nc) As Integer ‘存储每个城市的报名人数
Dim c(1 To nc) As Integer
Dim q(1 To nc) As Integer
Private Sub Form_Load()
‘本过程读取城市序号,姓名和参加次数的数据分别存储在数组city;pname和times中,并在List 1中显示,代码略
End Sub
Private Sub Command 1_Click( )
Dim i As Integer, j As Integer, k As Integer, t As Integer, pos As Integer
For i=1 To nc
b(i)=0
Next i
For i=1 To n ‘统计每个城市报名人数
b(k)=b(k)+1
Next i
k=1
For i=1 To nc
c(i)=k
k=k+b(1)
Next i
For i=1 To n
k=city(i)
c(k)=c(k)+1
Next i
pos=1
For i=1 To nc ‘对各城市报名数据按参加志愿服务的次数进行排序
For j=pos To pos +b(i)-2
k=fp(j, pos+b(i)-1)
t=q(k) : q(k )=q(j) : q(j)=t
Next j
pos=pos+b(i)
Next i
For i-1 To n
List 2.AddItem " " & city(q(i))& " " & pname(q(i))& " " & times(q(i))
Next i
End Sub
Function fp(head As Integer,tail As Integer)As Integer
Dim i As Integer,k As Integer
k=head
For i=
If times (q(i))>times (q(k)) Then k=i
Next i
fp=k
End Function