中文字幕精品无码一区二区,成全视频在线播放观看方法,大伊人青草狠狠久久,亚洲一区影音先锋色资源

2020版算法與程序設(shè)計(jì) 專項(xiàng)測(cè)試五 排序算法

資源下載
  1. 二一教育資源

2020版算法與程序設(shè)計(jì) 專項(xiàng)測(cè)試五 排序算法

資源簡(jiǎn)介

專項(xiàng)測(cè)試五 排序算法
一、選擇題
1.采用冒泡排序算法對(duì)數(shù)組a中的6個(gè)數(shù)據(jù)“8,20,5,17,30,9”進(jìn)行排序,部分程序代碼如下:
For i=1 To 2
 For j=6 To i+1 Step -1
If a(j)>a(j-1) Then
  t=a(j): a(j)=a(j-1): a(j-1)=t
End If
 Next j
Next i
運(yùn)行該程序,數(shù)據(jù)交換的次數(shù)為(  )
A.1 B.2
C.3 D.4
解析 本題主要考查的是冒泡排序的數(shù)據(jù)交換次數(shù)。根據(jù)語(yǔ)句“If a(j)>a(j-1)Then”可知,排序方式為從大到小排序(降序),排序是通過(guò)從后往前相鄰數(shù)據(jù)比較和交換進(jìn)行,排序過(guò)程如下:
原始數(shù)據(jù)
8
20
5
17
30
9
說(shuō)明
第1趟
30
8
20
5
17
9
30分別與17、5、20、8交換位置,因此,共交換4次
第2趟
30
20
8
17
5
9
17與5交換位置,20與8交換位置,因此,共交換2次
因此,答案為B。
答案 B
2.采用冒泡排序?qū)?shù)組d中元素進(jìn)行排序,第一趟排序后的結(jié)果為“10,25,15,30,50,38,60,55”,則d數(shù)組中元素的初始順序不可能是(  )
A.25,15,30,50,38,60,55,10
B.25,15,30,50,38,60,10,55
C.25,15,10,50,38,60,30,55
D.25,15,30,50,10,38,60,55
解析 觀察第一趟的排序可知,數(shù)據(jù)是按升序排序的,數(shù)據(jù)比較和交換是從后往前進(jìn)行的,本題可從選項(xiàng)模擬排序,D選項(xiàng)進(jìn)行第一趟排序后的數(shù)據(jù)順序?yàn)椤?0,25,15,30,50,38,55,60”,因此,答案為D。
答案 D
3.對(duì)數(shù)組元素b(1)~b(8)進(jìn)行降序排序,其排序算法的VB部分程序如下:
For x = 1 To 7
  w = x
  For y = x + 1 To 8

  Next y
  If w < > x Then
  Temp = b(x): b(i) = b(w): b(w) = temp
  End If
Next x
方框中應(yīng)填入的語(yǔ)句為(  )
A.If b(w) > b(y) Then w = y
B.If b(w) < b(y) Then w = y
C.If b(w) > b(y) Then w = x
D.If b(w) < b(y) Then w = x
解析 本題主要考查的是選擇排序算法的程序?qū)崿F(xiàn)。排序方式為降序,用變量w記錄當(dāng)前最大的元素所在的位置,當(dāng)在后面找到更多的數(shù)時(shí),則更新w的值,因此加框處的代碼為If b(w) < b(y) Then w = y,答案為B。
答案 B
4.有如下程序段:
For i = 1 To 4
For j = i + 1 To 5
 If a(j) > a(i) Then t = a(j): a(j) = a(i): a(i) = t
Next j
Next i
數(shù)組元素a(1)~a(5)的值依次為“15,4,12,7,9”,整個(gè)排序過(guò)程中,數(shù)組中數(shù)據(jù)比較次數(shù)和交換次數(shù)分別是(  )
A.比較11次,交換3次
B.比較11次,交換4次
C.比較10次,交換3次
D.比較10次,交換4次
解析 本題主要考查的是選擇排序算法。這是非優(yōu)化的選擇排序,每遍排序時(shí)將i位置的元素順序與它后面的元素比較,若比它大,則交換它們的位置。排序過(guò)程如下表所示:
原始數(shù)據(jù)
15
4
12
7
9
比較次數(shù)和交換次數(shù)
第一趟結(jié)束時(shí)
15
4
12
7
9
比較4次,無(wú)交換
第二趟
15
12
4
7
9
比較3次,交換1次
第三趟
15
12
9
4
7
比較2次,交換2次
第四趟
15
12
9
7
4
比較1次,交換1次
因此,共比較次數(shù)為4+3+2+1=10次,共交換次數(shù)為1+2+1=4次,答案為D。
答案 D
5.如下VB程序段:
st = “ ”
For i = 1 To 4
 For j = 1 To 10 - i
If d(j) < d(j + 1) Then
  temp = d(j): d(j) = d(j + 1): d(j + 1) = temp
End If
 Next j
 st = Str(d(j)) + st
Next i
Text1.Text = st
數(shù)組元素d(1)~d(10)中的數(shù)據(jù)依次為“21,8,12,16,26,71,65,89,42,36”,程序執(zhí)行后,在文本框Text1中顯示的內(nèi)容是(  )
A.8,12,16,21 B. 42,65,71,89
C.21,16,12,8 D.89,71,65,42
解析 本題考查的是冒泡排序算法的程序?qū)崿F(xiàn)。該程序的功能是對(duì)數(shù)據(jù)使用冒泡排序降序排序4趟,并將每趟得到的最小值存放在字符串st中,需注意的是語(yǔ)句st = Str(d(j)) + st,即將當(dāng)前得到的最小數(shù)拼接在字符串st的前面。因此,四趟排序結(jié)束后,變量st的值為“21,16,12,8”,答案為C。
答案 C
6.有如下VB程序段:
Private Sub Command1_Click()
 Dim a(1 To 100) As String, x As String, st As String
 Dim i As Integer, j As Integer, n As Integer
 x = Text1.Text
 n = Len(Text1.Text)
 For i = 1 To n
a(i) = Mid(x, i, 1)
 Next i
 For i = 1 To n - 1
For j = n To i + 1 Step -1
  If a(j) > a(j - 1) Then
st = a(j): a(j) = a(j - 1): a(j - 1) = st
  End If
Next j
 Next i
 st = “ ”
 For i = 1 To n
st = st + a(i)
 Next i
 Label1.Caption = st
End Sub
程序運(yùn)行時(shí),在文本框Text1中輸入“Text”(不包含雙引號(hào)),單擊按鈕Command1,則在標(biāo)簽Label1上顯示的內(nèi)容為(  )
A.Tetx B.Txte
C.xtTe D.xteT
解析 本題主要考查的是冒泡排序算法。本題的功能是將文本框中輸入的字符串進(jìn)行降序排序,字符比較大小實(shí)質(zhì)是比較字符的ASCII碼,小寫(xiě)字母的ASCII值大于大寫(xiě)字母,因此排序結(jié)果為xteT,答案為D。
答案 D
二、非選擇題
7.下面是一個(gè)采用拉鋸式排序法對(duì)數(shù)組元素按升序進(jìn)行排序的程序,所謂“拉鋸式排序法”是這一遍把最小的元素從下往上移動(dòng)到最上面的位置,下一遍則是從上往下把最大的元素移動(dòng)到最下面的位置。 程序運(yùn)行效果如下圖所示。
Private Sub Command1_Click()
 Dim a(10) As Integer, i As Integer
 Randomize ′產(chǎn)生不同的隨機(jī)數(shù)
 For i = 1 To 10
a(i) = Int(Rnd * 100) + 1
Text1.Text = Text1.Text & Str(a(i))
 Next i
 Call Shaker_sort(a()) ′調(diào)用Shaker_sort
 For i = 1 To 10
Text2.Text = Text2.Text & Str(a(i))
 Next i
End Sub
Private Sub Shaker_sort(k() As Integer)
 Dim i As Integer,c As Integer,d As Integer
 Dim t As Integer
 c = 1
 d = 10
 Do While c<=d
For i=______①______ ′從下往上把最小的元素移到最上面的位置
  If k(i-1)>k(i) Then
   t = k(i - 1):k(i- 1) = k(i):k(i) = t
  End If
Next i 
For i = c+1 To d-c ′從上到下把最大的元素移到最下面的位置
  If ______②______ Then
  t = k(i+1):k(i+1) = k(i):k(i) = t
  End If
Next i
c = c + 1
 ______③______
 Loop 
End Sub
解析 此題考查排序算法的分析和拓展。①代碼表示把當(dāng)前最小的元素從下往上移到最上面的位置,因此代碼為d to c+1 Step -1;②處所在循環(huán)的功能是從上到下把最大的元素移到最下面的位置,即當(dāng)前位置的元素比后面元素大時(shí),交換他們的位置,因此②處代碼為k(i)>k(i+1);每一趟排序?qū)⑴哦^尾兩個(gè)數(shù)的位置,因此,③處代碼為 d=d-1。
答案 ①d to c+1 Step -1 ②k(i)>k(i+1)
③d=d-1
8.小李基于冒泡排序算法編寫(xiě)了一個(gè)VB程序,功能如下:在文本框Text1中顯示排序前的數(shù)據(jù),單擊“排序”按鈕Command1,在文本框Text2中顯示剔除重復(fù)數(shù)據(jù)后的升序排序結(jié)果。程序運(yùn)行界面如下圖所示。
實(shí)現(xiàn)上述功能的VB程序如下,但加框處代碼有錯(cuò),請(qǐng)改正。
Const n = 10
Dim a(1 To n) As Integer
Private Sub Command1_Click()
 Dim i As Integer, j As Integer, t As Integer,bottom As Integer
 ′獲取排序前數(shù)據(jù)依次存儲(chǔ)在數(shù)組a中,并在文本框Text1中顯示。代碼略
 bottom = n
 i = 1
 Do While i <= bottom - 1
For j = bottom To i + 1 Step -1
  If  Then      ′①
t = a(j): a(j) = a(j - 1): a(j - 1) = t
  ElseIf a(j) = a(j - 1) Then   ′相鄰兩個(gè)數(shù)據(jù)相等,進(jìn)行剔除處理
         ′②
bottom = bottom - 1
  End If
Next j
i = i + 1
Loop
Text2.Text = “ ”
For i = 1 To bottom
 Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
程序中加框①處應(yīng)改正為_(kāi)________________________________________;
加框②處應(yīng)改正為_(kāi)______________________________________________。
解析 本題考查的是冒泡排序算法的變式。從數(shù)據(jù)交換的語(yǔ)句可知,加框①處的表達(dá)式為a(j)與a(j-1)的比較,因?yàn)榕判蚍绞綖樯颍虼思涌颌偬幍拇a修改為a(j)答案 ①a(j) a(j)
②a(j)=a(bottom)或t=a(j):a(j)=a(bottom):
a(bottom)=t
9.編寫(xiě)一個(gè)VB程序,實(shí)現(xiàn)功能為在n個(gè)數(shù)中尋找第m大的數(shù)。程序運(yùn)行時(shí),自動(dòng)生成n個(gè)三位數(shù)隨機(jī)整數(shù),并顯示在列表框List1中,單擊“排序”按鈕Command1,將數(shù)據(jù)進(jìn)行排序,并將排好序的數(shù)據(jù)顯示在列表框List2中,在文本框Text1中輸入m,單擊“計(jì)算”按鈕Command2,在文本框Text2中顯示第m大的數(shù)。程序運(yùn)行效果如下圖所示:
為實(shí)現(xiàn)上述功能的VB程序如下,在程序劃線處填入合適的代碼。
Dim d(1 To 20) As Integer
Const n = 20
Private Sub Form_Load()
 Dim i As Integer, j As Integer, t As Single
 List1.Clear
 Randomize
 For i = 1 To n
  ________①________
  List1.AddItem Str(d(i))
 Next i
End Sub
Private Sub Command1_Click()
 Dim i As Integer, j As Integer, t1 As Integer
 For i = 1 To n - 1
  For j =________②________
   If d(j) < d(j - 1) Then
    t1 = d(j): d(j) = d(j - 1): d(j - 1) = t1
   End If
  Next j
 Next i
 For i = 1 To n
  List2.AddItem Str(d(i))
 Next i
End Sub
Private Sub Command2_Click()
 Dim m As Integer
 m = Val(Text1.Text)
 Text2.Text = “第” + Str(m) + “ 大的數(shù)為:”
+ ________③________
End Sub
解析 本題采用冒泡排序算法。①處主要考查的是隨機(jī)函數(shù),要求生成三位數(shù)整數(shù)(即[100,999]范圍內(nèi)的整數(shù)),根據(jù)代碼“List1.AddItem Str(d(i))”可知,隨機(jī)數(shù)存儲(chǔ)在數(shù)組d中,第i個(gè)隨機(jī)數(shù)存儲(chǔ)在數(shù)組元素d(i)中,因此,①處代碼為d(i)=Int((Rnd * 900+100));②處所在的事件處理過(guò)程是冒泡排序,從交換語(yǔ)句可知,相鄰數(shù)據(jù)的比較是從后往前進(jìn)行的,因此②處代碼為n To i + 1 Step -1;③處代碼考查的是學(xué)生的思維能力,由于排序后的數(shù)據(jù)是升序,因此第m大的數(shù)為d(n-m+1),而不是d(n-m),因此③處代碼為Str(d(n - m + 1))。
答案 ①d(i) = Int((Rnd * 900 + 100))
②n To i + 1 Step -1 ③Str(d(n - m + 1))
10.小王編寫(xiě)了一個(gè)數(shù)據(jù)排序且將重復(fù)數(shù)刪除的VB程序。算法的基本思想:先隨機(jī)生成20個(gè)[1,50]區(qū)間內(nèi)的整數(shù),然后按照數(shù)據(jù)從低到高進(jìn)行排序,最后刪除重復(fù)數(shù)(相同的數(shù)據(jù)只保留一個(gè))。
程序運(yùn)行時(shí),單擊“生成隨機(jī)數(shù)”按鈕Command1,則生成20個(gè)[1,50]區(qū)間內(nèi)的整數(shù),存儲(chǔ)在數(shù)組a中,并顯示在文本框Text1中;單擊“排序并去重”按鈕Command2,則將數(shù)組a中數(shù)據(jù)進(jìn)行從低到高排序,并將排序結(jié)果顯示在文本框Text2中,將去重后結(jié)果顯示在文本框Text3中,在標(biāo)簽Label4中顯示共刪除的重復(fù)數(shù)個(gè)數(shù),程序運(yùn)行界面如下圖所示。
實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)回答下列問(wèn)題:
(1)若20個(gè)隨機(jī)數(shù)分別為:7,18,5,7,7,12,7,1,10,14,17,7,9,7,10,16,5,18,8,18,則去掉的重復(fù)數(shù)共________個(gè)。
(2)請(qǐng)?jiān)趧澗€處填入合適的代碼。
Dim a(1 To 20) As Integer
Const n = 20
Private Sub Command1_Click()
 Dim i As Integer, s1 As String
 Randomize
 s1 = “ ”
 For i = 1 To n
a(i) = Int(Rnd * 50 + 1)
s1 = s1 + Str(a(i))
 Next i
 Text1.Text = s1
End Sub
Private Sub Command2_Click()
 Dim i As Integer, j As Integer, p As Integer, tt As Integer
 Dim ans As Integer, s2 As String, s3 As String
 For i = 1 To n - 1
p = i
For j = n To i + 1 Step -1
  If a(j) < a(p) Then p = j
Next j
If a(i) < > a(p) Then
  tt = a(i)
  a(i) = a(p)
  a(p) = tt
End If
 Next i
 s2 = “ ”:s3 = “ ”
 ans = 0
 For i = 1 To n
s2 = s2 + Str(a(i))
 Next i
 Text2.Text = s2
 __________①__________
 s3 = Str(a(1))
 For i = 2 To n
If a(i) < > q Then
  __________②__________
  q = a(i)
Else
  __________③__________
End If
 Next i
 Text3.Text = s3
 Label4.Caption = Str(ans)
End Sub
解析 (1)重復(fù)的數(shù)有5、7、10、18,去掉1個(gè)5,5個(gè)7,1個(gè)10,2個(gè)18,因此共刪除的重復(fù)數(shù)有9個(gè)。(2)本題的算法思想是:首先將數(shù)據(jù)進(jìn)行升序排序,然后將相鄰的重復(fù)數(shù)刪除。因此,先將第一個(gè)數(shù)賦給變量q,然后從2個(gè)數(shù)開(kāi)始與q進(jìn)行比較,如果不相等,說(shuō)明當(dāng)前的數(shù)與它前面的數(shù)不重復(fù),則將它存放在字符串s3中,并把當(dāng)前數(shù)賦給q,繼續(xù)往下比較;如果相等,說(shuō)明當(dāng)前的數(shù)與它前面的數(shù)重復(fù),則不放入字符串s3,進(jìn)行重復(fù)數(shù)的計(jì)數(shù),根據(jù)語(yǔ)句“Label4.Caption = Str(ans)”可知,重復(fù)數(shù)的個(gè)數(shù)記錄在變量ans中,然后繼續(xù)往下比較。因此①處應(yīng)填入的語(yǔ)句為q=a(1),②處應(yīng)填入的語(yǔ)句為s3=s3+Str(a(i)),③處應(yīng)填入的語(yǔ)句為ans=ans+1。
答案 (1)9 (2)①q = a(1)
②s3 = s3 + Str(a(i)) ③ans = ans + 1
專項(xiàng)測(cè)試五 排序算法
一、選擇題
1.采用冒泡排序算法對(duì)數(shù)組a中的6個(gè)數(shù)據(jù)“8,20,5,17,30,9”進(jìn)行排序,部分程序代碼如下:
For i=1 To 2
 For j=6 To i+1 Step -1
If a(j)>a(j-1) Then
  t=a(j): a(j)=a(j-1): a(j-1)=t
End If
 Next j
Next i
運(yùn)行該程序,數(shù)據(jù)交換的次數(shù)為(  )
A.1 B.2
C.3 D.4
2.采用冒泡排序?qū)?shù)組d中元素進(jìn)行排序,第一趟排序后的結(jié)果為“10,25,15,30,50,38,60,55”,則d數(shù)組中元素的初始順序不可能是(  )
A.25,15,30,50,38,60,55,10
B.25,15,30,50,38,60,10,55
C.25,15,10,50,38,60,30,55
D.25,15,30,50,10,38,60,55
3.對(duì)數(shù)組元素b(1)~b(8)進(jìn)行降序排序,其排序算法的VB部分程序如下:
For x = 1 To 7
  w = x
  For y = x + 1 To 8

  Next y
  If w < > x Then
  Temp = b(x): b(i) = b(w): b(w) = temp
  End If
Next x
方框中應(yīng)填入的語(yǔ)句為(  )
A.If b(w) > b(y) Then w = y
B.If b(w) < b(y) Then w = y
C.If b(w) > b(y) Then w = x
D.If b(w) < b(y) Then w = x
4.有如下程序段:
For i = 1 To 4
For j = i + 1 To 5
 If a(j) > a(i) Then t = a(j): a(j) = a(i): a(i) = t
Next j
Next i
數(shù)組元素a(1)~a(5)的值依次為“15,4,12,7,9”,整個(gè)排序過(guò)程中,數(shù)組中數(shù)據(jù)比較次數(shù)和交換次數(shù)分別是(  )
A.比較11次,交換3次
B.比較11次,交換4次
C.比較10次,交換3次
D.比較10次,交換4次
5.如下VB程序段:
st = “ ”
For i = 1 To 4
 For j = 1 To 10 - i
If d(j) < d(j + 1) Then
  temp = d(j): d(j) = d(j + 1): d(j + 1) = temp
End If
 Next j
 st = Str(d(j)) + st
Next i
Text1.Text = st
數(shù)組元素d(1)~d(10)中的數(shù)據(jù)依次為“21,8,12,16,26,71,65,89,42,36”,程序執(zhí)行后,在文本框Text1中顯示的內(nèi)容是(  )
A.8,12,16,21 B. 42,65,71,89
C.21,16,12,8 D.89,71,65,42
6.有如下VB程序段:
Private Sub Command1_Click()
 Dim a(1 To 100) As String, x As String, st As String
 Dim i As Integer, j As Integer, n As Integer
 x = Text1.Text
 n = Len(Text1.Text)
 For i = 1 To n
a(i) = Mid(x, i, 1)
 Next i
 For i = 1 To n - 1
For j = n To i + 1 Step -1
  If a(j) > a(j - 1) Then
st = a(j): a(j) = a(j - 1): a(j - 1) = st
  End If
Next j
 Next i
 st = “ ”
 For i = 1 To n
st = st + a(i)
 Next i
 Label1.Caption = st
End Sub
程序運(yùn)行時(shí),在文本框Text1中輸入“Text”(不包含雙引號(hào)),單擊按鈕Command1,則在標(biāo)簽Label1上顯示的內(nèi)容為(  )
A.Tetx B.Txte
C.xtTe D.xteT
二、非選擇題
7.下面是一個(gè)采用拉鋸式排序法對(duì)數(shù)組元素按升序進(jìn)行排序的程序,所謂“拉鋸式排序法”是這一遍把最小的元素從下往上移動(dòng)到最上面的位置,下一遍則是從上往下把最大的元素移動(dòng)到最下面的位置。 程序運(yùn)行效果如下圖所示。
Private Sub Command1_Click()
 Dim a(10) As Integer, i As Integer
 Randomize ′產(chǎn)生不同的隨機(jī)數(shù)
 For i = 1 To 10
a(i) = Int(Rnd * 100) + 1
Text1.Text = Text1.Text & Str(a(i))
 Next i
 Call Shaker_sort(a()) ′調(diào)用Shaker_sort
 For i = 1 To 10
Text2.Text = Text2.Text & Str(a(i))
 Next i
End Sub
Private Sub Shaker_sort(k() As Integer)
 Dim i As Integer,c As Integer,d As Integer
 Dim t As Integer
 c = 1
 d = 10
 Do While c<=d
For i=______①______ ′從下往上把最小的元素移到最上面的位置
  If k(i-1)>k(i) Then
   t = k(i - 1):k(i- 1) = k(i):k(i) = t
  End If
Next i 
For i = c+1 To d-c ′從上到下把最大的元素移到最下面的位置
  If ______②______ Then
  t = k(i+1):k(i+1) = k(i):k(i) = t
  End If
Next i
c = c + 1
 ______③______
 Loop 
End Sub
8.小李基于冒泡排序算法編寫(xiě)了一個(gè)VB程序,功能如下:在文本框Text1中顯示排序前的數(shù)據(jù),單擊“排序”按鈕Command1,在文本框Text2中顯示剔除重復(fù)數(shù)據(jù)后的升序排序結(jié)果。程序運(yùn)行界面如下圖所示。
實(shí)現(xiàn)上述功能的VB程序如下,但加框處代碼有錯(cuò),請(qǐng)改正。
Const n = 10
Dim a(1 To n) As Integer
Private Sub Command1_Click()
 Dim i As Integer, j As Integer, t As Integer,bottom As Integer
 ′獲取排序前數(shù)據(jù)依次存儲(chǔ)在數(shù)組a中,并在文本框Text1中顯示。代碼略
 bottom = n
 i = 1
 Do While i <= bottom - 1
For j = bottom To i + 1 Step -1
  If  Then      ′①
t = a(j): a(j) = a(j - 1): a(j - 1) = t
  ElseIf a(j) = a(j - 1) Then   ′相鄰兩個(gè)數(shù)據(jù)相等,進(jìn)行剔除處理
         ′②
bottom = bottom - 1
  End If
Next j
i = i + 1
Loop
Text2.Text = “ ”
For i = 1 To bottom
 Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
程序中加框①處應(yīng)改正為_(kāi)________________________________________;
加框②處應(yīng)改正為_(kāi)______________________________________________。
9.編寫(xiě)一個(gè)VB程序,實(shí)現(xiàn)功能為在n個(gè)數(shù)中尋找第m大的數(shù)。程序運(yùn)行時(shí),自動(dòng)生成n個(gè)三位數(shù)隨機(jī)整數(shù),并顯示在列表框List1中,單擊“排序”按鈕Command1,將數(shù)據(jù)進(jìn)行排序,并將排好序的數(shù)據(jù)顯示在列表框List2中,在文本框Text1中輸入m,單擊“計(jì)算”按鈕Command2,在文本框Text2中顯示第m大的數(shù)。程序運(yùn)行效果如下圖所示:
為實(shí)現(xiàn)上述功能的VB程序如下,在程序劃線處填入合適的代碼。
Dim d(1 To 20) As Integer
Const n = 20
Private Sub Form_Load()
 Dim i As Integer, j As Integer, t As Single
 List1.Clear
 Randomize
 For i = 1 To n
  ________①________
  List1.AddItem Str(d(i))
 Next i
End Sub
Private Sub Command1_Click()
 Dim i As Integer, j As Integer, t1 As Integer
 For i = 1 To n - 1
  For j =________②________
   If d(j) < d(j - 1) Then
    t1 = d(j): d(j) = d(j - 1): d(j - 1) = t1
   End If
  Next j
 Next i
 For i = 1 To n
  List2.AddItem Str(d(i))
 Next i
End Sub
Private Sub Command2_Click()
 Dim m As Integer
 m = Val(Text1.Text)
 Text2.Text = “第” + Str(m) + “ 大的數(shù)為:”
+ ________③________
End Sub
10.小王編寫(xiě)了一個(gè)數(shù)據(jù)排序且將重復(fù)數(shù)刪除的VB程序。算法的基本思想:先隨機(jī)生成20個(gè)[1,50]區(qū)間內(nèi)的整數(shù),然后按照數(shù)據(jù)從低到高進(jìn)行排序,最后刪除重復(fù)數(shù)(相同的數(shù)據(jù)只保留一個(gè))。
程序運(yùn)行時(shí),單擊“生成隨機(jī)數(shù)”按鈕Command1,則生成20個(gè)[1,50]區(qū)間內(nèi)的整數(shù),存儲(chǔ)在數(shù)組a中,并顯示在文本框Text1中;單擊“排序并去重”按鈕Command2,則將數(shù)組a中數(shù)據(jù)進(jìn)行從低到高排序,并將排序結(jié)果顯示在文本框Text2中,將去重后結(jié)果顯示在文本框Text3中,在標(biāo)簽Label4中顯示共刪除的重復(fù)數(shù)個(gè)數(shù),程序運(yùn)行界面如下圖所示。
實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)回答下列問(wèn)題:
(1)若20個(gè)隨機(jī)數(shù)分別為:7,18,5,7,7,12,7,1,10,14,17,7,9,7,10,16,5,18,8,18,則去掉的重復(fù)數(shù)共________個(gè)。
(2)請(qǐng)?jiān)趧澗€處填入合適的代碼。
Dim a(1 To 20) As Integer
Const n = 20
Private Sub Command1_Click()
 Dim i As Integer, s1 As String
 Randomize
 s1 = “ ”
 For i = 1 To n
a(i) = Int(Rnd * 50 + 1)
s1 = s1 + Str(a(i))
 Next i
 Text1.Text = s1
End Sub
Private Sub Command2_Click()
 Dim i As Integer, j As Integer, p As Integer, tt As Integer
 Dim ans As Integer, s2 As String, s3 As String
 For i = 1 To n - 1
p = i
For j = n To i + 1 Step -1
  If a(j) < a(p) Then p = j
Next j
If a(i) < > a(p) Then
  tt = a(i)
  a(i) = a(p)
  a(p) = tt
End If
 Next i
 s2 = “ ”:s3 = “ ”
 ans = 0
 For i = 1 To n
s2 = s2 + Str(a(i))
 Next i
 Text2.Text = s2
 __________①__________
 s3 = Str(a(1))
 For i = 2 To n
If a(i) < > q Then
  __________②__________
  q = a(i)
Else
  __________③__________
End If
 Next i
 Text3.Text = s3
 Label4.Caption = Str(ans)
End Sub

展開(kāi)更多......

收起↑

資源列表

<pre id="tfb94"><li id="tfb94"></li></pre>

<bdo id="tfb94"><rt id="tfb94"></rt></bdo>
  • <menu id="tfb94"><dl id="tfb94"></dl></menu><i id="tfb94"><acronym id="tfb94"><sub id="tfb94"></sub></acronym></i>

    1. 主站蜘蛛池模板: 阜城县| 绩溪县| 五指山市| 澳门| 乌审旗| 西林县| 朝阳市| 逊克县| 营山县| 云浮市| 寻乌县| 凤城市| 鹤庆县| 祁门县| 乌拉特中旗| 泸水县| 临泽县| 高碑店市| 凤山县| 东城区| 平邑县| 贵定县| 巴中市| 临夏县| 赞皇县| 宣化县| 柯坪县| 镇坪县| 新龙县| 贺州市| 平湖市| 万荣县| 剑川县| 光泽县| 神木县| 体育| 太谷县| 沭阳县| 开平市| 大理市| 罗平县|