Private Sub Command1_Click()
Dim s As String, i As Integer, d As Integer
d=0
s = Text1.Text
For i = Len(s) To 1 Step -1
d = (d \ 2 + Val(Mid(s, i, 1)) * 2 ^ (Len(s) - 1))
Next i
Text2.Text = Str(100-d)
End Sub
Private Sub Command2_Click( )
Dim f As Integer, i As Long, n As Long
Dim pi As Double, s As Double
s = 0: n = Val(Text1.Text)
For i = 2 To 2*n ①
s = s + ②
Next i
pi = 3 + s * 4
Label2.Caption = Str(pi)
End Sub
有如下VB程序段:
For i = 1 To n
For j = n * (i - 1) + 1 To ①
For k = ② To ③
If a(k) > a(k + 1) Then
t =a(k): a(k) = a(k + 1): a(k + 1) = t
End If
Next k
Next j
Next i
上述程序段划线处的表达式正确的是( )
Dim a(1 To 6)As Integer
Randomize
a(1)=Int(Rnd*2)+1
For i=2 To 6
a(i)=Int(Rnd*i)*2+1
If a(i)< a(i-1)Then
a(i)= a(i)+ a(i-1)
End If
Next i
执行该程序段后,数组元素a(1)到a(6)的值可能是( )
i=1:j=8:k=0
Key=Int(Rnd*50)*2+3
Do While i<=j
m=(i+j+1)\2
If Key=a(m)Then
Exit Do
ElseIf Key <a(m)Then
i=m+1:k=k+1
Else
j=m-1:k=k-1
End If
Loop
Text1.Text=Str(k)
数组元素a(1)到a(8)的值依次为“88,66,50,46,24,20,18,6”,执行该程序段后,k的值不可能是( )
图 b
Dim cj(1000) As Integer, n As Integer
Private Sub btncmd1_Click( )
Dim i As Integer, j As Integer
Dim k As Integer ,tmp As Integer
For i = 2 To n
tmp = cj(i)
j = BinarySearch
For k = i To j + 1 Step -1
cj(k) = cj(k - 1)
Next k
cj(k) = tmp
Next i
For i = 1 To n
List1.AddItem Str(cj(i))
Next i
End Sub
Private Sub btncmd2_Click()
Dim i As Integer
i = BinarySearch(Val(Text1.Text), 1, n)
Label2.Caption = "共有" ++ "位同学大于等于该成绩。"
End Sub
Function BinarySearch(key As Integer,i As Integer,j As Integer) As Integer
Dim m As Integer
Do While i <= j
m = (i + j) \ 2
If cj(m) >= key Then
j = m - 1
Else
i = m + 1
End If
Loop
BinarySearch =
End Function
Private Sub Form_Load()
'从数据库获取n个成绩存储在 cj(1) ~cj(n)组中,代码略
End Sub
记录各个考生的笔试和面试得分,根据得分分别计算笔试和面试的名次(得分越高排名越前,得分相同,名次也相同);再根据笔试和面试的名次计算总分(总分=笔试名次+面试名次);最后计算总分的名次(总分越低排名越前,得分相同,名次相同)。程序设计过程中,为节省内存,尽可能少使用数组,编程实现上述功能:窗体加载时,自动从数据库读取考生的笔试和面试得分,显示在列表框 List1 中;在文本框 Text1 中输入录取人数 n,单击“统计”按钮 Command1,在标签 Label1、Label2 和 Label3 中分别输出笔试、面试、总分前 n 名的考生编号。程序运行界面如图所示。
Const n = 9
'数组 xh、df 和 mc 分别存储选手编号、得分和名次;df 数组的 1~n 元素存储笔试相关数据;n+1~2n 元素存储对应选手的面试相关数据;2n+1~3n 元素存放两项考试总分相关数据
Dim xh(1 To n * 3) As Integer, df(1 To n * 3) As Integer, mc(1 To n * 3) As Integer
Dim i As Integer, j As Integer, t As Integer
'本过程从数据库读取各选手的选手编号、得分数据,分别存储在数组 xh、df 中
Private Sub Form_Load( )
List1.Clear
List1.AddItem "选手" + " " + "笔试" + " " + "面试"
For i = 1 To n
xh(i) = i: xh(n + i) = i
Next i
For i = 1 To n
List1.AddItem Str(xh(i)) + " " + Str(df(i)) + " " + Str(df(n + i))
Next i
End Sub
Private Sub Command1_Click()
Dim c As Integer
c = Val(Text1.Text)
Label1.Caption = "笔试前" & Str(c) & "名:" & pm(1, n, c, False)
Label2.Caption = "面试前" & Str(c) & "名:" & pm(n + 1, 2 * n, c, False)
For i = 1 To n
xh(2 * n + i) = i: df(2 * n + i) = 0: mc(2 * n + i) = 0
Next i
For i = 1 To 2 * n
df(2 * n + xh(i)) = df(2 * n + xh(i)) + mc(i)
Next i
Label3.Caption = "总分前" & Str(c) & "名:" & pm
End Sub
Function pm(ks As Integer, js As Integer, num As Integer, fx As Boolean) As String
For i = ks To js - 1
For j = js To ks + 1 Step -1
If (fx And df(j) < df(j - 1)) OrThen
t = df(j): df(j) = df(j - 1): df(j - 1) = t
t = xh(j): xh(j) = xh(j - 1): xh(j - 1) = t
End If
Next j
Next i
mc(ks) = 1: pm = Str(xh(ks))
For i = ks + 1 To js
If df(i) = df(i - 1) Then
Else
mc(i) = i - ks + 1
End If
If mc(i) <= num Then pm = pm & "," & Str(xh(i))
Next i
End Function