做法是比较中点与当前查找范围第1个数大小关系,用对分法使查找范围逼近拐点;当余下数组只有二个元素时,可根据此二元素大小关系得到拐点的位置。
依据上述描述设计了VB程序,界面如图所示。请回答下列问题:
Dim a(1 To 20) As Integer
Private Sub Command1_Click()
Dim Low As Integer, High As Integer
Low = 1
High = 20
Do While High > Low
If ① Then '当只余下两个元素时
If ② Then
Label1.Caption = "拐点元素为" + Str(a(1)) + ",下标为" + Str(1)
Else
Label1.Caption = "拐点元素为" + Str(a(High)) + ",下标为" + Str(High)
End If
Exit Do
End If
If a(m) >= a(Low) Then
Low = m
Else
③
End If
Loop
End Sub
Private Sub Form_Load()
'生成循环递增的数组a,代码略
End Sub
① ② ③
S=“7218634594”:n=Len(s)
t=0
For i=1 To n-1
a(i)=Val(Mid(s,i,2))
Next i
For i=1 To n-2 Step 2
k=i
For j=i+2 To n-1 Step 2
If a(i)<a(k)Then k=j
Next j
If k<>i Then
temp=a(i):a(i)=a(k):a(k)=temp:t=t+1
End If
Next i
Text1.Text=Str(t)
运行程序段后, t的值为( )
实现上述功能的程序如下,请回答下列问题:
Private Sub Com1_Click( )
Dim s As String
Dim flag As Boolean
Dim n As Integer, i As Integer
s = Text1.Text
_________ ‘①
flag = True
i = 1
Do While i <= n \ 2
If Mid(s, i, 1) < >________________ Then ‘②
flag = False
Exit Do '退出Do循环
End If
i = i + 1
Loop
If flag Then
Label2.Caption = s + "是回文字符串"
Else
Label2.Caption = s + "不是回文字符串"
End If
End Sub
下划线①处应填入代码。
下划线②处应填入代码。
实现上述功能的 VB 程序如下,请回答下列问题:
Dim b(1 To 10) As Integer
Dim left1 As Integer, right1 As Integer Private Sub Cd1_Click()
Dim i As Integer, j As Integer
For i = 1 To 10
b(i) = Int(Rnd * 100) + 10 List1.AddItem Str(b(i))
Next i left1 = 1 right1 = 10
End Sub
Private Sub Cd2_Click()
Dim i As Integer, j As Integer, t As Integer
Do While left1 < right1
For j = left1 To right1 - 1
If b(j) > b(j + 1) Then
t = b(j): b(j) = b(j + 1): b(j + 1) = t
End If
Next j
For j = right1 To ‘改错
If b(j) < b(j - 1) Then
t = b(j): b(j) = b(j - 1): b(j - 1) = t end If
Next j
right1 = ‘填空
Loop
‘将排序结果显示在列表表框List2中,代码略
End Sub
t=0
For i=1 To 4
flag=False
For j=5 To i+1 Step-1
If a(j)>a(i) Then flag=True
t=t+1
temp=a(j):a(j)=a(i):a(i)=temp
End If
Next j
If flag=False Then Exit For
Next i
程序运行时,在文本框Text1中依次输入一个整数后按回车键,输入的整数顺序显示在列表框List1中,单击“排序”按钮 Command1,排序后的数据显示在列表框List2中程序运行界面如图所示,回答以下各题:
Dim a (1 To 20) As Integer, n As Integer ‘ 定义全局变量
Sub jh (x As Integer, y As Integer)
‘jh 为自定义过程,其功能是交换变量x、y的值 Dim temp As Integer
temp = x
x = y
y = temp
End Sub
Private Sub Command1_Click ( )
Dim i As Integer, temp As Integer
Dim swap As Boolean
swap = True
Do while swap = True
swap = False
For i = 1 To n-1
If a(i) > a(i+1) Then
Call jh (a(i), a(i+1)) ‘ 调用过程jh,对两个数进行交换
①
End If
Next i
Loop
For i = 1 To n
List2. AddItem a (i)
Next i
End sub
‘ 输入数据,存储在数组a中,并显示在列表框List1中
Private Sub Text1_ Key Press (KeyAscii As Integer)
If KeyAscii = 13 Then ‘表示按下回车键
n = n+1
②
List1. AddItem a(n)
Text1. Text =“ ”
End if
End sub
Private Sub Form_ Load ( )
n = 0
End sub
① ②
实现上述功能的部分代码如下:
key = Val(Text2.Text) i = 1: j = 10
Do While i <= j m = (i + j) 2
if i = m + 1
Then
Else
j = m - 1 End If
Loop
Label1.Caption = "大于等于" + Str(key) + "的成绩有" + Str(num) + "个"
上述程序段2个方框处的代码分别为( )。
Key=Val(Text1.Text)
i=1:j=10: f=false: n=0
Do While i<= j and f=false
n=n+1
m=(i+j+1)\2
if a(m)=key then f=true
if a(m)>Key Thenj=m 1Elsei=m+1
Loop
数组元素a(1)到a(10)的值依次为“4,7,10,12,15,19,20,23,28,30”。在文木框Text1中输入待查找的数,执行该程序段后n的值为4,则key值可能的是( )
n = Val(Text1.Text)
x = Val(Text2.Text)
k = 0
For i = 1 To n
①
Do While temp <> 0
y= temp Mod 10
If ② Then k = k + 1
temp = temp \ 10
Loop
Next
Label1.Caption = Str(k)
s = Text1.Text
For i = 1 To Len(s)
ch = Mid(s, i, 1)
nk = Asc(ch) - i
If nk < 0 Then nk = nk + 127
Text2.Text = Text2.Text + Chr(nk)
Next i
已知大写字母A的ASCII码值为65,小写字母a的ASCII码值为97,在文本框Text1中输入内容“yhT5”,执行程序段后,文本框Text2显示的内容为( )
Dim i As Integer,Sum As Integer
Dim a(1 To 11)As Integer
i=10:a(11)=49
Do While i>=1
a(i)=a(i+1)-1
If a(i)Mod 3=0 Then Sum=Sum+a(i)
i=i-1
Loop
Text1.Text=Str(Sum)
该程序段运行后,文本框Text1中显示的内容是( )
For i=8 To 2 Step-1
For j=i-2 To 1 Step -2
If a(j)<a(i) And i Mod 2=1 Then
t=a(j):a(j)=a(i):a(i)=t
ElseIf a(j)>a(i) And i Mod 2=0 Then
t=a(j):a(j)=a(i):a(i)=t
End If
Next j
Next i
数组元素a(1)到a(8)的值依次为20、15、62、26、74、64、 20、32,执行该程序段后,数组元素的值依次是:( )
For i = 1 To 8
a(i) = Int(Rnd * 10) * 2 + 1
Next i
For i = 1 To 3
For j = 1 To 8 - i * 2
If a(j) > a(j + 2) Then
t = a(j): a(j) = a(j + 2): a(j + 2) = t
End If
Next j
Next i
执行该程序段后,在下列选项中,a(1)~a(8)各元素可能的值是( )
请根据算法将下列程序补充完整。
Private Sub Command1_Click()
Label3.Caption = ""
For i = 1 To 50
a(i) =
If i Mod 10 = 0 Then
Label3.Caption = Label3.Caption + CStr(a(i)) + vbCrLf '换行
Else
Label3.Caption = Label3.Caption + CStr(a(i)) + " "
End If
Next i
End Sub
Private Sub Command2_Click()
Label4.Caption = ""
n = 0
For i = 1 To 50
For k = 2 To a(i) - 1
If Then Exit For
Next k
If k = a(i) Then
n = n + 1
If n = 1 Then b(n) = a(i)
For j = 1 To n - 1
If b(j) = a(i) Then: Exit For
If j = n - 1 Then b(n) = a(i)
Next j
End If
Next i
For j = 1 To n
Label4.Caption = Label4.Caption + + " "
Next j
1)让各店的外卖员送货上门,对于第i个菜需要花费a(i)的时间。
2)自己去餐厅取回家,每次出门仅能取回一道菜,对于第i个菜,从出门到回家整个过程需要花费b(i)的时间。
每家餐厅都有各自的外卖员送菜,并且无论老张是否在家,他们都能把菜送到。为了节省时间,能由外类员送的菜尽量由外卖负各自送达,现在需要知道最少花费多少时间能集齐所有的菜。
比如n=4,a(i)=(30,70,40,50),b(1)=(20,10,20,30},那么花费时间少的方案是第一个菜和第三个菜让让外卖员送,第二个菜和第四个菜老张自己取,一共需要40分钟。
设计VB程序计算最少时间,界面如下图
实现上述功能的VB程序如下,回答以下问题:
(单选,填字母:A .Form1/B .Label /C .TextBox / D .CommandButton)
Const n=4
Dim a(1 To n) As Integer 'n道菜由每家餐厅外卖员送达耗时
Dim b(1 To n) As Integer 'n道菜自取耗时
Private Sub Form Load)
'产生外卖送货各道菜耗时,并存储到数组a中,并显示
'产生自取各道菜耗时,并存储到数组b中,并显示
'代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, max As Integer, ans As Integer
Dim left As Integer, right As Integer, m As Integer
For i=1 To n
If a(i) > max Then '外卖员送菜的最长时间
Next i
left= 1: right = max: ans = max
Do While left <= right
m= (left + right) \ 2
If Then
ans = m: right=m-1
Else
left=m+1
End If
Loop
Lab3. Caption=“最快”+ Str(ans) + “分钟”
End Sub
Private Function check(t As Integer) As Boolean
Dim total As Integer
check = False
For i =1 To n
If a(i) > t Then
total ='如果外卖来不及就自己取
End If
Next i
If Then check = True
End Function
Key = Val(Text1.Text):s = 0: i = 1: j = 7
Do While i <= j
m = (i + j) \ 2
s = s + m
If a(m) > Key Then j = m - 1 Else i = m + 1
Loop
Text2.Text = Str(s)
数组元素a(1)到a(7)的值依次为“12,15,18,20,20,20,29”,在文本框Text1中输入待查数,执行该程序段后,则文本框 Text2 中显示的内容不可能的是( )