17.小王編寫了一個(gè)依據(jù)成績(jī)計(jì)算名次的VB程序,成績(jī)?yōu)?到100之間的整數(shù)。算法的基本思想:先統(tǒng)計(jì)每個(gè)分?jǐn)?shù)的個(gè)數(shù),然后按照分?jǐn)?shù)從高到低依次計(jì)算每個(gè)有效分?jǐn)?shù)(該分?jǐn)?shù)的個(gè)數(shù)不為0)對(duì)應(yīng)的名次,分?jǐn)?shù)相同時(shí)名次并列。最高分為第1名,該分?jǐn)?shù)的名次與個(gè)數(shù)之和為下一個(gè)有效分?jǐn)?shù)的名次,以此類推。程序用數(shù)組A存放每個(gè)分?jǐn)?shù)對(duì)應(yīng)的個(gè)數(shù),數(shù)組B存放每個(gè)分?jǐn)?shù)對(duì)應(yīng)的名次。例如,下表中最高分100有2個(gè),并列第1名,則分?jǐn)?shù)96的名次為分?jǐn)?shù)100的名次加上分?jǐn)?shù)100的個(gè)數(shù),即第3名。
分?jǐn)?shù) |
100 |
99 |
98 |
97 |
96 |
95 |
94 |
… |
0 |
個(gè)數(shù)(A數(shù)組) |
2 |
0 |
0 |
0 |
1 |
0 |
3 |
… |
0 |
名次(B數(shù)組) |
1 |
|
|
|
3 |
|
4 |
… |
|
程序運(yùn)行時(shí),學(xué)生數(shù)據(jù)顯示在列表框List1中,單擊“計(jì)算”按鈕Command1,計(jì)算結(jié)果顯示在列表框List2中,程序運(yùn)行界面如圖所示。
實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)回答下列問題:
(1)如上表所示,若分?jǐn)?shù)93的個(gè)數(shù)為2,則該分?jǐn)?shù)對(duì)應(yīng)的名次為
。
(2)請(qǐng)?jiān)跈M線處填入合適的代碼。
Dim sName(1 To 50)As String'存放學(xué)生姓名
Dim sScore(1 To 50)As Integer'存放學(xué)生分?jǐn)?shù)
Dim recCount As Integer'存放學(xué)生人數(shù)
Private Sub Form_Load ( )
'本過程從數(shù)據(jù)庫(kù)中讀取學(xué)生數(shù)據(jù),存儲(chǔ)在相應(yīng)的變量中,并在List1中顯示
'代碼略
End Sub
'整數(shù)轉(zhuǎn)換成長(zhǎng)度固定的字符串
Function ads(x As Integer,n As Integer) As String
Dim sx As String,nx As Integer,i As Integer
sx=Str(x):nx=Len(sx)
For i=1 To n-nx
sx=““+sx
Next i
①
End Function
Private Sub Command1_Click ( ?。?br />Dim A(0 To 100)As Integer'存放每個(gè)分?jǐn)?shù)的個(gè)數(shù)
Dim B(0 To 100)As Integer'存放每個(gè)分?jǐn)?shù)的名次
Dim mc As Integer,score As Integer,i As Integer
For i=0 To 100
A(i)=0
Next i
For i=1 To recCount'計(jì)算每個(gè)分?jǐn)?shù)的個(gè)數(shù)
②
Next i
mc=1
For i=100 To 0 Step-1'計(jì)算每個(gè)分?jǐn)?shù)的名次
If A(i)<>0 Then
B(i)=mc
③
End If
Next i
List2.Clear
List2.AddItem“姓名 分?jǐn)?shù) 名次“
List2.AddItem“----------------“
For i=1 To recCount
score=sScore(i)
mc=B(sScore(i))
List2.AddItem sName(i)+ads(score,5)+“第“+ads(mc,3)+“名“
Next i
End Sub