Dim conn As New ADODB.Connection
Di mrs As New ADODB.Recordset
conn.ConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;DataSource=” & App.Path & “\词库.accdb”
conn.Open
Set rs.Active Connection=conn
rs.Open “SELECT*FROM words”
n=0
Do While Not rs.EOF
n=n+1
en(n) =rs.Fields(1)
ch(n) =rs.Fields(2)
rs.MoveNext
Loop
rs.Close
conn.Close
下列说法中正确的是( )
①连接的数据库文件名是“\词库.accdb”
②程序运行后,变量n的值表示该数据表中记录的总数
③数组en(n)所存储的是数据表第2个字段的字段值
④“ConnectionString”是conn对象的属性值
⑤查询的数据表名称是“words”
⑥省略语句“rs.MoveNext”,程序也可以正常运行
str 1=“go it for!”
str 2=Mid(str 1, s, t-s+1)
len 1=Len(str 1) :len 2=Len(str 2)
c=c Mod len 2
For i=1To c
str 2=Mid(str 2, len 2, 1) +Mid(str 2, 1, len 2-1)
Next i
str 1=Mid(str 1,1,s-1) +str 2+Mid(str 1,t+1,len 1-t)
Dim d(1 to 5) As Integer, i As Integer
For i= 1 To 8
a(i) =Int(Rnd*10) + 1
Next i
For i=1 To 5 Step 2
k=i
For j=i+2 To 7 Step 2
If a(j) <a(k) Or a(j) =a(k) And a(j+1) <a(k+1)
Next j
Then k=j
If i<>k Then
t=a(i):a(i)=a(k):a(k)=t
t=a(i+1):a(i+1)=a(k+1):a(k+1)=t
EndIf
Next i
运行该程序段后,数组a各元素的值可能为( )
Private Sub Command1_Click()
Dim a( 1 To 6) As Integer, i As Integer
For i= 1 To 6
a(i) =Int(Rnd*5) *2+ 1
Next i
For i=2 To 6 Step 2
a(i) =cha(a(i-1) , a(i) )
Next i
End Sub
Function cha(x As Integer, y As Integer) As Integer
Dim t As Integer
If x<y Then
t=x:x=y:y=t
cha=y
Else
cha=x-1
End If
EndFunction
执行该程序段后,在下列选项中,a(1)~a(6)各元素值可能的是( )
租车原则是首先考虑自带经费多的部分同学优先租相对花费少的自行车,尽量少花学校经费以达到最多同学租车的结果。比如学生带的钱为9,3,6,7,5,租车的钱分别需要15,8,12,6,9,11,如果5个人都能租车,至少需要学校提供经费为(6-3)+(8-5)+(9-6)+(11-7)+(12-9)=16,如果学校经费只有10元,则自带钱最少的同学不租车,这时所需学校经费为(6-5)+(8-6)+(9-7)+(11-9)=7。计算过程中采用二分查找法来判断租用自行车的人数能否达到最大值。
Dim a( 1 To 100) As Integer, b( 1 To 100) As Integer
Dim n As Integer, m As Integer
Private Sub Command1_Click()
Dim left As Integer, right As Integer, mid As Integer, an s As Integer
n=Val(Text 1.Text) :m=Val(Text 2.Text)
‘从数据库读取n位同学带的钱存储在数组a,m辆自行车租用的钱存储在数组b,代码略。
Call px(a(),n)
Call px(b(),m)
left =1:right=n:ans= 0
Do While left<=right
mid=(left+right) \ 2
If check(mid) =True Then
①
right =mid-1
Else
left =mid+ 1
End If
Loop
Label 1.Caption=“能租车的最大人数为”+Str(ans)
End Sub
Sub px(d() As Integer, s As Integer)
Dimi as integer, j as integer, k as integer
For i=2Tos
k=d(i)
j=i-1
Do While k<d(j)
d(j+1)=d(j)
j=j-1
If j=0ThenExitDo
Loop
②
Next i
End Sub
Function check(mid As Integer) As Boolean
Dim cnt As Integer, jf As Integer, t As Integer
cnt=Val(Text 3.Text)
For t=1Ton-mid+ 1
If b(t) >a(mid+t-1) Then jf=b(t) -a(mid+t-1) Else jf= 0
③
Next t
If cnt>= 0 Then check=True
End Function
① ② ③
算法描述如下:
1)从第一行开始向下进行累加,累加和若大于之前的最大和,则记录此时的最大和及结束位置;
2)若累加和等于之前的最大和,但元素个数大于之前的最大和的元素个数,则记录此连续子矩阵的结束位置;
3)若累加和小于0,则重新开始记录;
4)若有符合条件的多个连续子矩阵,则输出最先找到的子矩阵。
小俞编写了一个实现该功能的VB程序,窗体加载时生成m*n个序列数据,依次存放在数组a,并显示在列表框List1中,在文本框Text1中输入该矩阵限定区域的左上角位置,在文本框Text2中输入右下角位置,单击“计算”按钮Command1后,找出连续和最大的子矩阵,在标签Label3上显示最大连续子矩阵和,在Label 4上显示该连续子矩阵的元素个数,在Label 5上显示该连续子矩阵开始与结束位置。程序运行界面如图所示。
Const m=12:Const n= 6
Dim a(1 Tom*n) As Integer
Private SubForm_Load()
‘生成m*n个数据,并显示在列表框List 1,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, temp As Integer
Dim length As integer, begin As Integer, sum As Integer
Dim ks As String, js As String
Dim xy( 1 to 4) As Integer, h sum(1tom*n) As Integer
‘读取文本框Text 1的数值分别存储到数组xy(1)、xy(2),读取文本框Text 2的数值分别存储到数组xy(3)、xy(4),xy(1)、xy(3)表示所在行,xy(2)、xy(4)表示所在列,代码略
For i=1 To xy(3) -xy(1) + 1
h sum(i) = 0
Next i
‘求限定区域内每行数据之和
For i=1 To xy(3) -xy(1) + 1
For j= 1 To ①
h sum(i) =h sum(i) +a((xy(1) +i-2) *n+xy(2) +j-1)
Next j
Next i
‘找出最大连续之矩阵和
temp =0:sum=0:length=0:begin= 0
For i=1 To xy(3)-xy(1) + 1
If temp+h sum(i) >sum Then
sum=temp+h sum(i)
length=i-begin
k=i
ElseIf temp+h sum(i) =sum And ② Then
length=i-begin
k=i
End If
If temp+h sum(i) < 0 Then
begin=i
temp= 0
Else
temp=temp+h sum(i)
End If
Next i
ks=“(“ ③ ”+Str(xy(2) ) +) ” ‘开始位置
js=“(“+Str(k+xy(1) -1) +” “+Str(xy(4) ) +”) ” ‘结束位置
Label 3.Caption=“最大子矩阵和为:”+Str(Sum)
Label 4.Caption=“子矩阵中的元素个数为:”+Str(length*(xy(4) -xy(2) +1) )
Label 5.Caption=“子矩阵位置为:”+ks+“,”+js
End Sub
① ② ③