小王編寫了一個(gè)依據(jù)成績(jī)統(tǒng)計(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)回答下列問(wèn)題:
(1)如表示,若分?jǐn)?shù)93的個(gè)數(shù)為2,則該分?jǐn)?shù)對(duì)應(yīng)的名次為
7
7
.
(2)請(qǐng)?jiān)跈M線處填入合適的代碼
Dim sName(1to 50)As String‘存放學(xué)生姓名
Dim sScore(1to 50)As Integer’存放學(xué)生分?jǐn)?shù)
Dim recount As Integer’存放學(xué)生人數(shù)
Private Sub Form_load ( ?。?br />‘本過(guò)程從數(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=1to n-nx
sx=””+sx
Next i
①
abs=sx
abs=sx
End Function
Private Sub Command1_click( ?。?br />Dim A(0to 100)As Integer’存放每個(gè)分?jǐn)?shù)的個(gè)數(shù)
Dim B(0to 100)As Integer’存放每個(gè)分?jǐn)?shù)的名次
Dim mc As Integer,score As integer,i As Integer
For i=0to 100⑧
A(i)=0
Next i
For i=1to recount’計(jì)算每個(gè)分?jǐn)?shù)的個(gè)數(shù)
②
A(sScore(i))=A(sScore(i))+1
A(sScore(i))=A(sScore(i))+1
Next i
mc=1
For i=100to 0step-1’計(jì)算每個(gè)分?jǐn)?shù)名次
If A(i)<>0Then
B(i)=mc
③
mc=mc+A(i)或mc=B(i)+A(i)
mc=mc+A(i)或mc=B(i)+A(i)
End if
Next i
List2.clear
List2.a(chǎn)dditem“姓名 分?jǐn)?shù) 名次”
List2.a(chǎn)dditem”--------”
For i=1to recount
score=sScore(i)
mc=B(sScore(i))
list2.a(chǎn)dditem sName(i)+ads(score,5)+”第”+ads(mc,3)+”名”
Next i
End Sub.