记录各个考生的笔试和面试得分,根据得分分别计算笔试和面试的名次(得分越高排名越前,得分相同,名次也相同);再根据笔试和面试的名次计算总分(总分=笔试名次+面试名次);最后计算总分的名次(总分越低排名越前,得分相同,名次相同)。程序设计过程中,为节省内存,尽可能少使用数组,编程实现上述功能:窗体加载时,自动从数据库读取考生的笔试和面试得分,显示在列表框 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