试题

试题 试卷

logo

题型:单选题 题类:常考题 难易度:困难

算法与程序设计

某对分查找算法的VB程序段如下:

k = Val(Text1.Text)

i = 1: j = 6: Label1.Caption = ″ ″: f = False

Do While i <= j And Not f

m = (i + j) \ 2

If a(m) = k Then f = True

If a(m) > a(i) Then

If a(i) <= k And k < a(m) Then j = m - 1 Else i = i + 1

Else

If a(m) < k And k <= a(j) Then i = i + 1 Else j = j - 1

End If

Label1.Caption = Label1.Caption + Str(a(m))

Loop

数组元素a(1)到a(6)的值依次为“58,66,72,24,35,40”,在文本框Text1中输入的值为35,执行该程序段,标签Label1中显示的值是(  )

A、72 35 B、24 35 C、72 24 35 D、72 24 24 35
举一反三
某商场元月举行VIP客户积分换购活动,VIP客户根据卡内积分多少可换取不同额度的代金券。假设VIP客户共有n名,VIP卡内积分存放在数据库“customer.accdb”的Integral表中,换购活动的VB程序代码如下,程序运行时界面如第7题图所示。工作人员在文本框Text1中输入VIP卡号后,单击“换购”按钮Command1,在文本框Text2中输出VIP客户的积分数,在标签Label3中显示可以换购的代金券额度,积分清零。按此要求编写程序如下, 但加框处代码有错,请改正。

Dim ID( ) As String    '用于存放客户卡号

Dim total( ) As Long      '用于存放积分数

Dim sc As Long                  'VIP客户人数

Dim jf As Long                  '积分数

Private Sub Command1_Click( )

  Dim k As String                '客户卡号

  Dim q As Long               '代金券额度

  Dim i As Long

  k = Text1.Text

  For i = 1 To sc     ’顺序查找

      If ID(i) = k Then

          jf = total(i)     

          cash(jf) = q               ’ ①

          Exit For

      End If

      Text2.Text = Str(jf)

      Label3.Caption = “您可换购的代金券总额为:”+ Str(q) + “元”

    Next i

  End If

End Sub

' cash函数用于计算VIP客户可换购的代金券额度

Function cash(jf As Long) As Long

  If jf >= 2000 And jf <= 20000 Then

       Cash = jf\2000*10

ElseIf jf > 20000 And jf <= 50000 Then

       Cash = jf\2000*11

ElseIf jf > 50000 And jf <= 100000 Then

       Cash = jf\2000*12

ElseIf jf > 100000 And jf <= 150000 Then

       Cash = jf\2000*13

 Else jf > 150000 Then         ’ ②

       Cash = jf\2000*14

End If

End Function

Private Sub Form_Load()

   Dim conn As New ADODB.Connection, rs As New ADODB.Recordset

   Dim intSQL As Long

   conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + App.Path + "\ customer.accdb"

    conn.Open

   intSQL = "SELECT score  FROM Integral" 

     Set rs.ActiveConnection = conn

     rs.CursorType = adOpenStatic

     rs.Open intSQL

     sc = 0

     Do While Not rs.EOF

       sc = sc + 1

       total(sc) = rs.Fields("score")

       rs.MoveNext

     Loop

rs.Close

     conn.Close

     Set rs = Nothing

     Set conn = Nothing

End Sub

返回首页

试题篮