資源簡介 專項(xiàng)測試七 數(shù)據(jù)存儲(chǔ)與數(shù)組應(yīng)用一、選擇題1.有如下VB程序段:Dim a(1 To 8) As Integer, i As Integer, c As Integer a(1) = 1 : a(2) = 2 : a(3) = 3 : a(4)=4 : a(5) = 7 : a(6) = 8∶a(7) = 15: a(8) = 25c=0For i = 3 To 8 If a(i - 2) + a(i - 1) = a(i) Then c = c + 1 Next iText1. Text = Str(c)該程序段運(yùn)行后,文本框Text1中顯示的內(nèi)容是( )A.1 B.2 C.3 D.4解析 本題主要考查的是數(shù)組操作。本程序的功能是:統(tǒng)計(jì)滿足條件“當(dāng)前項(xiàng)等于前兩項(xiàng)之和”的數(shù)組元素個(gè)數(shù),數(shù)組元素a(3)、a(5)、a(7)滿足條件,因此答案為C。答案 C2.有如下VB程序段:a(1) = 1a(2) = 3 For i = 3 To 10 a(i) = 2 * a(i - 1) - a(i - 2) Next i執(zhí)行該程序段后,數(shù)組元素a(8)的值為( )A.11 B.13 C.15 D.17解析 本題主要考查的是數(shù)組操作。程序運(yùn)行過程中數(shù)組元素a(1)~a(10)的值如下表所示:a(1)a(2)a(3)a(4)a(5)a(6)a(7)a(8)a(9)a(10)135791113151719因此,數(shù)組元素a(8)的值為15,答案為C。答案 C3.有如下VB程序段:a(1) = 1: a(2) = 2 b(1) = 1: b(2) = 2 For i = 3 To 10 a(i) = a(i - 1) + a(i - 2) b(i) = b(i - 1) + a(i) Next i執(zhí)行該程序段后,數(shù)組元素b(5)的值為( )A.5 B.10 C.18 D.31解析 本題主要考查的是數(shù)組操作。程序運(yùn)行過程中數(shù)組a、b各元素的值如下表所示:a(1)a(2)a(3)a(4)a(5)a(6)a(7)a(8)a(9)a(10)123581321345589b(1)b(2)b(3)b(4)b(5)b(6)b(7)b(8)b(9)b(10)1251018315286141230從上表可知b(5)=18,因此答案為B。答案 B4.有如下VB程序段: For i = 2 To 5 j = i k = a(j) Do While a(j - 1) < k And j > 1 a(j) = a(j - 1) j = j - 1 Loop a(j) = k Next i數(shù)組元素a(1)~a(5)的值依次為“79,20,34,56,61”,該程序段運(yùn)行后,數(shù)組元素a(1)~a(5)的值依次是( )A.79,56,34,20,61 B.79,61,56,34,20C.20,34,56,61,79 D.20,34,56,79,61解析 本題主要考查的插入排序算法。插入排序算法的關(guān)鍵是找到要插入的位置,本程序?yàn)榻敌蚺判颍惴ㄋ枷霝椋簩?dāng)前要插入的數(shù)(目標(biāo)數(shù))與它前面的數(shù)進(jìn)行比較,若比目標(biāo)數(shù)小,則將該數(shù)往后移動(dòng)一個(gè)位置,繼續(xù)往前比較,直到找到一個(gè)比它大的數(shù)或和前面的數(shù)全部比較完畢,最后將目標(biāo)數(shù)插入。排序結(jié)束后數(shù)組元素a(1)~a(5)的值依次是“79,61,56,34,20”,因此答案為B。答案 B5.有如下VB程序段: s = “ ” For i = 1 To 5For j = 1 To a(i)s = s + Str(i)Next j Next i Label1.Caption = s數(shù)組元素a(1)~a(5)的值依次為“2,0,3,2,0”,執(zhí)行該程序段,標(biāo)簽Label1中顯示的內(nèi)容為( )A.1 2 1 2 3 1 2 B.1 1 3 3 3 4 4C.2 2 3 3 3 2 2 D.1 1 0 3 3 3 4 4 0解析 本題主要考查的是二重循環(huán)和數(shù)組。需要注意的是:循環(huán)體的語句是“s = s + Str(i)”,因此答案為B。答案 B6.有如下程序段:Private Sub Command1_Click() Dim d(1 To 10) As Integer, f(1 To 10) As Boolean Dim i As Integer, ans As Integer ′給數(shù)組元素d(1)~d(10)賦值,代碼略 For i = 1 To 10f(i) = False Next i For i = 2 To 10If d(i) - d(i - 1) > 0 Then f(i) = True d(i) = d(i) - d(i - 1)End If Next i For i = 1 To 10If f(i) Then ans = ans + 1 Next i Label1.Caption = Str(ans)End Sub程序運(yùn)行時(shí),數(shù)組元素d(1)~d(10)的賦值分別為:25、15、10、8、1、7、8、11、16、5,則標(biāo)簽Label1中顯示的內(nèi)容為( )A.3 B.4 C.5 D.6解析 本題主要考查的是程序的綜合運(yùn)用能力。程序執(zhí)行過程各變量的變化如下表所示:ii=1i=2i=3i=4i=5i=6i=7i=8i=9i=10d(i)251510817811165d(i)>d(i - 1)?NNNNNYYYYNf(i)FFFFFTTTTFd(i)6297因此答案為B。答案 B7.實(shí)現(xiàn)某算法的部分VB 程序段如下:i = 1Do While i <= 9If a(i) <> 0 Thenj = 10Do While j > i If a(j) = a(i) Then a(j) = 0 j = j - 1LoopEnd Ifi = i + 1LoopFor i = 1 To 10If a(i) <> 0 Then Text1.Text= Text1.Text+Str(a(i))Next i數(shù)組元素 a(1)到a(10)的數(shù)據(jù)依次為“4,1,6,4,4,9,1,7,6”,則程序運(yùn)行后,文本框Text1中顯示的內(nèi)容是( )A.4 9 1 7 6 B.4 1 6 9 7C.6 7 1 9 4 D.7 9 6 1 4解析 程序的功能是從后向前查找重復(fù)的數(shù),并把重復(fù)的數(shù)改為0。i表示趟數(shù),從1循環(huán)到9。如果當(dāng)前這趟中的元素不為0,從最后一個(gè)元素開始查找,如果與當(dāng)前的元素相等,賦值為0。如i=1時(shí),a(1)=4,從最后一個(gè)元素開始,把5號4號數(shù)組元素的值賦值為0。答案 B二、非選擇題8.楊輝三角,又稱賈憲三角形,帕斯卡三角形,是二項(xiàng)式系數(shù)在三角形中的一種幾何排列。小楊同學(xué)設(shè)計(jì)了以下程序用來輸出楊輝三角,程序界面如下圖所示。程序運(yùn)行時(shí),在文本框Text1里輸入需要輸出的楊輝三角的行數(shù)n,單擊“打印”按鈕后,在列表框List1中輸出n行的楊輝三角。(1)要使程序運(yùn)行時(shí)自動(dòng)清空文本框Text1中的內(nèi)容,則應(yīng)在事件處理過程________(填字母:A.Form_Load/B.Text1_Click/C.Command1_Click)中編寫相應(yīng)代碼。(2)實(shí)現(xiàn)上述功能的VB程序如下,請?jiān)趧澗€處填入適當(dāng)?shù)拇a。Private Sub Command1_Click() Dim n As Integer, i As Integer, j As Integer, a(0 To 30) As Long Dim s As String List1.Clear ____①____ a(1) = 1 List1.AddItem Str(a(1)) For i = 2 To n a(1) = 1 s = “ ” For j = i To 1 Step -1 ____②____ s = Str(a(j)) + “ ” + s Next j ____③____ Next iEnd Sub解析 本題主要考查循環(huán)結(jié)構(gòu)和數(shù)組的應(yīng)用。本題要輸出n行的楊輝三角圖形,一般采用二維數(shù)組來實(shí)現(xiàn),這里使用一維數(shù)組實(shí)現(xiàn),需要從楊輝三角的規(guī)律來傳推理計(jì)算公式。①處代碼表示在文本框Text1中輸入n的值,變量n的類型為整型,因此①處代碼為n = Val(Text1.Text);②處代碼為計(jì)算公式為a(j) = a(j - 1) + a(j);③處表示將變量s的值輸出在列表框List1中,因此代碼為List1.AddItem s。答案 (1)A (2)①n = Val(Text1.Text)②a(j)=a(j-1)+a(j) ③List1.AddItem s9.小明編寫了一個(gè)特殊的排序程序,功能如下:隨機(jī)產(chǎn)生10個(gè)1~100之間的整數(shù),存儲(chǔ)在數(shù)組a并顯示在列表框 List1 中,單擊“排序”按鈕 Command1 后,在列表框 List2 中顯示降序排序后的結(jié)果,運(yùn)行界面如圖所示。特殊排序算法描述:(1)排序過程中不改變數(shù)據(jù)的位置,即保持原有數(shù)據(jù)的位置不變。(2)引入數(shù)組 index,用數(shù)組元素index(i)記錄第i大的數(shù)據(jù)在數(shù)列中的位置。(3)在數(shù)組 a 中找出最大數(shù)據(jù),將其所在位置存放在數(shù)組元素 index(1)中,然后在余下的數(shù)據(jù)中找出最大數(shù)據(jù),將其所在位置存放在index(2)中,以此類推,直到完成所有數(shù)據(jù)的位置存放。排序完畢, a(index(i))成為降序序列, 即 a(index(1)) ≥ a(index(2))≥a(index(3)) ≥…… ≥a(index(n))。如 n=5 時(shí),數(shù)組 a 排序后各變量值如下表所示。i12345a(i)17199136index(i)21435a(index(i))191713196實(shí)現(xiàn)上述功能的 VB 程序如下,在程序劃線處填入合適的代碼。Const n = 10, maxn = 100Dim a(1 To n) As Integer, index(1 To n) As Integer, flag(1 To n) As BooleanPrivate Sub Form_Load() Dim i As Integer Randomize For i = 1 To nflag(i) = Falsea(i) = Int(Rnd() * maxn) + 1List1.AddItem Str(a(i)) Next iEnd SubPrivate Sub Command1_Click() Dim i As Integer, j As Integer, k As Integer For i = 1 To n For j = 1 To n If flag(j) = False Then______①____:Exit For Next j For m = k + 1 To n If ____②____Then k = m Next m index(i) = k flag(k) = True Next i For i = 1 To n List2.AddItem Str(____③____) Next iEnd Sub解析 本題主要考查了數(shù)組的靈活應(yīng)用。本題將數(shù)組元素index(i)作為數(shù)組a的下標(biāo),用數(shù)組index記錄數(shù)據(jù)在數(shù)列中的位置,最大值、次大值、……依次存放在index(1)、index(2)、……,因此a(index(1))、a(index(2))、……成為降序序列。答案 ①k = j ②flag(m)=False And a(m)>a(k) ③a(index(i))10.小華有k個(gè)好朋友(編號為1~k),有一天,她的k個(gè)朋友向她要照片,可她手上只有n張照片(n(1)她根據(jù)和每個(gè)朋友的親密關(guān)系依次給他們打分;(分值為1~20間的整數(shù))(2)將k個(gè)朋友的打分按照分?jǐn)?shù)的個(gè)位數(shù)數(shù)字分成10組(0~9),再給每組的朋友生成一個(gè)隨機(jī)附加分;(分值為1~10間的整數(shù))(3)每個(gè)朋友的總分為親密關(guān)系分加上附加分,總分最高的前n個(gè)朋友將得到照片,若分?jǐn)?shù)相同,則編號小的朋友優(yōu)先。程序運(yùn)行時(shí),在文本框Text1中輸入照片數(shù),在文本框Text2中輸入朋友數(shù),單擊“讀取數(shù)據(jù)”按鈕Command1,將讀取與每個(gè)朋友的親密關(guān)系分和每組的附加分,并將親密關(guān)系分和附加分分別存放在數(shù)組a和數(shù)組c中,最后在列表框List1和List2中顯示;單擊“分組”按鈕Command2,將親密關(guān)系分進(jìn)行分組,分組情況顯示在列表框List3;單擊“分發(fā)”按鈕Command3,將分發(fā)情況(分到照片的朋友編號和總分)顯示在列表框List4中。程序運(yùn)行效果如下圖所示。小利依據(jù)上述描述設(shè)計(jì)了如下VB程序。請回答下列問題:(1)程序運(yùn)行時(shí),若在文本框Text2中輸入8,讀取的數(shù)據(jù)如圖所示,則第8位得到照片的朋友編號為________。(2)請?jiān)趧澗€處填入合適的代碼。Dim n As Integer, k As IntegerDim a(1 To 200) As Integer, b(0 To 9) As String, c(0 To 9) As IntegerDim d(1 To 10) As Integer, id(1 To 100) As Integer ′數(shù)組d存放總分,id存放朋友的編號Private Sub Command1_Click() ′讀取數(shù)據(jù)并存放在相應(yīng)的數(shù)組中,代碼略End SubPrivate Sub Command2_Click() Dim i As Integer, m As Integer, s As String For i = 1 To n m = ____①____ d(i) = ____②____ b(m) = b(m) + Str(a(i)) Next i For i = 0 To 9 If b(i) < > “ ” Then List3.AddItem Str(i) + “:” + b(i) Next iEnd SubPrivate Sub Command3_Click() Dim i As Integer, j As Integer, temp As Integer For i = 1 To n - 1For j = n To i + 1 Step -1 If ____③____ Thentemp = d(j): d(j) = d(j - 1): d(j - 1) = temptemp = id(j): id(j) = id(j - 1): id(j - 1) = temp End IfNext j Next i For i = 1 To k List4.AddItem Str(id(i)) + “ 號朋友的總分 ” + Str(d(i)) Next iEnd Sub解析 本題主要考查的多數(shù)組間相關(guān)操作。解決此問題的關(guān)鍵是分清各個(gè)數(shù)組的功能及存儲(chǔ)的數(shù)據(jù)情況,本題共有5個(gè)數(shù)組(a、b、c、d和id數(shù)組)。(1)根據(jù)圖中信息可知,已得到照片的朋友的編號為:2、3、4、5、7、8、10,還剩下編號為1、6、9三位朋友,因此將分別計(jì)算出這三位朋友的得分即可求出答案,1號朋友的得分為9分(5+4),6號朋友的得分為7分(1+6),9號朋友的得分為4分(2+2),因此第8位得到照片的朋友是1號。(2)根據(jù)親密分的末尾數(shù)字來進(jìn)行分類,即求出親密分的個(gè)位數(shù),親密分存儲(chǔ)在數(shù)組a中,因此①處填入的代碼為m = a(i) Mod 10;總分存放在d數(shù)組中,分組打分存儲(chǔ)在c數(shù)組中,總分為親密分+分組打分,因此d(i)=a(i)+c(m);③處語句為按照總分降序排序,因此此處應(yīng)填入的語句為d(j) > d(j - 1)。答案 (1)1 (2)①m = a(i) Mod 10 ②a(i) + c(m) ③d(j) > d(j - 1)11.小李與小王合作編寫一個(gè)成績統(tǒng)計(jì)的VB程序:小王編寫一個(gè)過程,該過程從數(shù)據(jù)庫讀取某一指定科目的相關(guān)數(shù)據(jù),存儲(chǔ)在數(shù)組a中;小李編寫一個(gè)過程,該過程依據(jù)數(shù)組a中的相關(guān)數(shù)據(jù)統(tǒng)計(jì)各班平均分。小李與小王約定的數(shù)組a各元素含義如表所示。數(shù)組元素數(shù)組元素的含義a(1)存儲(chǔ)班級數(shù)na(2)從a(2)到a(n+1) 依次存儲(chǔ)第1、2、…第n個(gè)班級人數(shù)…a(n+1)a(n+2)從a(n+2) 依次存儲(chǔ)第1班每個(gè)學(xué)生的單科成績、第2班每個(gè)學(xué)生的單科成績、…第n班每個(gè)學(xué)生的單科成績……程序功能如下:在文本框Text1中輸入科目名稱,單擊“讀取數(shù)據(jù)庫”按鈕Command1,程序從數(shù)據(jù)庫讀取數(shù)據(jù);單擊“開始統(tǒng)計(jì)”按鈕Command2,程序進(jìn)行統(tǒng)計(jì)處理,結(jié)果輸出在列表框List1中。程序運(yùn)行界面如下圖所示。實(shí)現(xiàn)上述功能的VB程序如下,請回答下列問題:(1)根據(jù)程序運(yùn)行界面中的數(shù)據(jù)及數(shù)組a各元素的含義,數(shù)組元素a(5)的值為________(填寫數(shù)值)。(2)分析程序,可知數(shù)據(jù)庫的文件名為________。(3)請?jiān)趧澗€處填入合適的代碼。Dim a(1 To 600) As Integer ′數(shù)組大小滿足處理要求Private Sub Command1_Click() ′本過程由小王完成,從數(shù)據(jù)庫讀取指定科目的各相關(guān)數(shù)據(jù),存儲(chǔ)在數(shù)組a中 Dim conn As New ADODB.Conncction Dim rs As New ADODB.Recordset conn ConncctionString=“provider=Microsoft.ACE.OLEDB.12.0;data source=”+ “Score.accdb” conn.Open Set rs.ActiveConnection = conn ′本過程的其他語句略End SubPrivate Sub Command2_Click() ′依據(jù)數(shù)組a中的相關(guān)數(shù)據(jù)統(tǒng)計(jì)各班級平均分 Dim i As Integer, j As Integer, n As Integer Dim p As Integer, sum As Integer, aver As Single ________①________ p = n + 2 For i = 1 To n sum = 0 For j = 1 To a(i + 1) ________②________p = p + 1 Next j aver = sum / a(i + 1) List1.AddItem Str(i) + “ ” + Str(a(i + 1)) + “ ” + Str(aver) Next iEnd Sub解析 (1)根據(jù)數(shù)組元素定義可知,數(shù)組元素a(5)存儲(chǔ)的是4班的人數(shù),因此為答案33。(2)從代碼“conn ConncctionString=”provider=Microsoft.ACE.OLEDB.12.0;data source=“+ ”Score.accdb“”可知,數(shù)據(jù)庫的文件名為Score.accdb。(3)變量n代表班級數(shù),班級數(shù)存放在數(shù)組元素a(1)中,因此①處代碼為n=a(1);i代表班級號,程序②處循環(huán)的功能是求班級i的總分,班級i的學(xué)生成績存儲(chǔ)在數(shù)組a(p)中,因此②處代碼為sum=sum+a(p)。答案 (1)33 (2)Score.accdb (3)①n=a(1) ②sum=sum+a(p)12.由數(shù)組a生成數(shù)組b的方法描述如下:(1)將數(shù)組a中的n個(gè)元素依次分割出若干個(gè)數(shù)據(jù)塊,每個(gè)數(shù)據(jù)塊有m×m個(gè)元素,m最大值為8,最小值為2。分割時(shí),按盡可能大的數(shù)據(jù)塊進(jìn)行分割;(2)對每個(gè)分割出的數(shù)據(jù)塊用“方陣轉(zhuǎn)換法”進(jìn)行轉(zhuǎn)換,每次轉(zhuǎn)換后得到的數(shù)據(jù)塊依次存儲(chǔ)在數(shù)據(jù)b中;(3)數(shù)組a分割后的剩余元素(個(gè)數(shù)小于4),直接依序存儲(chǔ)到數(shù)組b中。例如n=140時(shí),可依次分割出3個(gè)數(shù)據(jù)塊,元素的個(gè)數(shù)分別為64(8×8)、64(8×8)、9(3×3),剩余元素為3個(gè)。“方陣轉(zhuǎn)換法”過程如下:將數(shù)據(jù)塊中m×m個(gè)元素按行序排列成一個(gè)數(shù)字方陣,從該數(shù)字方陣中按列序得到轉(zhuǎn)換后元素的次序。以3×3數(shù)據(jù)塊為例,轉(zhuǎn)換過程如下圖所示:← 轉(zhuǎn)換前元素次序轉(zhuǎn)換后元素次序→小明依據(jù)上述描述設(shè)計(jì)了如下VB程序。請回答下列問題:(1)當(dāng)n=120時(shí),分割出的第3個(gè)數(shù)據(jù)塊元素個(gè)數(shù)為________。(2)請?jiān)趧澗€處填入合適的代碼。Const n = 120Dim a(1 To n) As IntegerDim b(1 To n) As IntegerPrivate Sub Command1_Click() Dim m As Integer, i As Integer Dim Start As Integer ′當(dāng)前未分割數(shù)據(jù)的第1個(gè)元素下標(biāo) Dim Left As Integer ′當(dāng)前未分割數(shù)據(jù)的個(gè)數(shù) Dim pa As Integer ′數(shù)組a的下標(biāo) Dim pb As Integer ′數(shù)組b的下標(biāo) ′讀取n個(gè)轉(zhuǎn)換前的數(shù)據(jù),依次存儲(chǔ)到a(1)、a(2)、……a(n)中,代碼略 m = 8 Start = 1 Left = n Do While Left > 3 If Left < m * m Thenm = ______①______ Else pa = Start pb = Start For i = 1 To m * m b(pb) = a(pa) pb = pb + 1 If i Mod m = 0 Then ______②______ Else pa = pa + m End If Next i ______③______ Start = Start + m * m End If Loop For i = Start To nb(i) = a(i) Next i′依次輸出轉(zhuǎn)換后數(shù)據(jù)b(1)、b(2)、……b(n),代碼略End Sub解析 (1)n=120 時(shí),可依次分割出 3 個(gè)數(shù)據(jù)塊,元素的個(gè)數(shù)分別為 :64(8×8)、49(7×7)、4(2×2),剩余元素為 3 個(gè)。因此,第 3 個(gè)數(shù)據(jù)塊為 4 個(gè)元素。(2)剩余數(shù)量 Left < m * m,意味著當(dāng)前剩余數(shù)量不夠分割成數(shù)據(jù)塊m ×m ,則下一次嘗試分割數(shù)據(jù)塊(m-1)×(m-1),……,因此①處應(yīng)填入的語句為m=m-1。例如 left=90 ,m=8 ,首先分割成 8 ×8 ,left=90-64=26,不夠分割成8×8,則依次嘗試 7 ×7、6 ×6、5 ×5,可分割成5 ×5,最后剩下一個(gè)元素,直接存儲(chǔ)到數(shù)組b中;下面分析當(dāng) m=5 時(shí)轉(zhuǎn)置處理:當(dāng) i=1 時(shí),執(zhí)行 b(1)=a(1),然后推算出下一個(gè)存入b數(shù)組的元素a[pa],pa=pa+m=6;當(dāng) i=2 時(shí),執(zhí)行 b(2)=a(6),然后推算出下一個(gè)存入b數(shù)組的元素a[pa],pa=pa+m=11;當(dāng) i=3 時(shí),執(zhí)行 b(3)=a(11),然后推算出下一個(gè)存入b數(shù)組的元素a[pa],pa=pa+m=16;……當(dāng) i=5 時(shí),執(zhí)行 b(5)=a(21),此時(shí),i mod m=0,則下一個(gè)該存入b數(shù)組的元素是什么呢?即pa=?,很顯然是第2個(gè)數(shù),即pa=start+1;當(dāng)i=10時(shí),執(zhí)行 b(10)=a(22),此時(shí),i mod m=0,則下一個(gè)該存入b數(shù)組的元素是什么呢?即pa=?,很顯然是第3個(gè)數(shù),即pa=start+2;如此繼續(xù),當(dāng)i=15時(shí),pa=start+3,i=20時(shí),pa=start+4,因此,可得出公式pa=start+im,因此②處應(yīng)填入的語句為pa=start+im。分割之后剩余元素的數(shù)量left=left-m*m,則下一個(gè)數(shù)據(jù)塊的頭元素的位置start為Start + m * m。答案 (1)4 (2)①m=m-1 ②pa=start+im或pa=pa-(m-1)*m+1 ③left=left-m*m或left=n-start-m*m+113.小王編寫了一個(gè)VB程序,該程序的功能是:有15個(gè)數(shù)形成環(huán)狀,現(xiàn)要分別找出3個(gè)相鄰的數(shù),使其相加之和最大或最小。如15個(gè)數(shù)依次為:18 14 4 26 1 13 19 14 13 28 5 26 15 8 30,則相鄰三數(shù)之和最大為62(30+18+14),相鄰三數(shù)之和最小為31(4+26+1)。程序運(yùn)行時(shí),先隨機(jī)生成15個(gè)[1,30]區(qū)間內(nèi)的整數(shù),存儲(chǔ)在數(shù)組a中,并在文本框Text1中顯示;單擊“計(jì)算”按鈕Command2,則在標(biāo)簽Label4中顯示連續(xù)三數(shù)最大和,在標(biāo)簽Label5中顯示連續(xù)三數(shù)最小和,程序運(yùn)行界面如圖所示。實(shí)現(xiàn)上述功能的VB程序如下,請回答下列問題:(1)若15個(gè)數(shù)分別為:30 8 26 7 5 8 16 16 21 18 24 30 26 27 30,則相鄰三數(shù)之和最大最小值分別為____________________。(2)請?jiān)趧澗€處填入合適的代碼。Dim a(0 To 14) As IntegerPrivate Sub Form_Load() ′隨機(jī)生成15個(gè)數(shù),存儲(chǔ)在數(shù)組元素a(0)~a(14)中,并顯示在文本框Text1中End SubPrivate Sub Command1_Click() Dim i As Integer, j As Integer, k As Integer, max As Integer, min As Integer Dim sum As Integer, maxi As Integer, mini As Integer sum = 0 max = 0 min = 5000 For i = 0 To 14 j = (i + 1) Mod 15 ______①______ sum = a(i) + a(j) + a(k) If sum > max Then max = sum ______②______ End If If sum < min Then min = sum mini = i End IfNext iLabel4.Caption=a(maxi)&“+”&a((maxi+1) Mod 15)&“+”&a((maxi+2) Mod 15)& “=”&maxLabel5.Caption=a(mini)&“+”&a((mini+1) Mod 15)&“+”&a((mini+2) Mod 15)& “=” &minEnd Sub答案 (1)87、20 (2)①k = (i + 2) Mod 15②maxi = i14.產(chǎn)生10個(gè)[20-30]之間的不重復(fù)的正整數(shù),并顯示在列表框List1中給20~30號數(shù)組元素編號。如果數(shù)組元素為0,表示未產(chǎn)生,并把該數(shù)組元素的標(biāo)志修改為1,直到產(chǎn)生10個(gè)數(shù)為止。例如產(chǎn)生的隨機(jī)數(shù)是21,由先判斷S(21)的值,如果大于0,表示該元素已經(jīng)產(chǎn)生,如果s(21)=0表示該元素未產(chǎn)生,把該數(shù)產(chǎn)生的順序存儲(chǔ)。Private Sub Command1_Click() Dim s(20 to 30) As Integer For i = 20 To 30 s(i) = 0 Next i Randomize i = 1 Do While i <= 10______①________List2.AddItem Str((x)) ′顯示程序過程中產(chǎn)生的數(shù)If s(x) = 0 Then ______②________ i =i+1 End If Loop For i =20 to 30 If s(i) > 0 Then List1.AddItem Str(i) + “產(chǎn)生順序:” + Str(s(i)) Next iEnd Sub解析 變量x表示產(chǎn)生的[20-30]之間的隨機(jī)數(shù),因此產(chǎn)生該數(shù)的表達(dá)式為Int(Rnd*11+20)。數(shù)組元素s(x)表示數(shù)x產(chǎn)生的順序,當(dāng)他等于0時(shí),表示該數(shù)還未產(chǎn)生。答案 ①Int(Rnd*11+20) ②s(x) = i專項(xiàng)測試七 數(shù)據(jù)存儲(chǔ)與數(shù)組應(yīng)用一、選擇題1.有如下VB程序段:Dim a(1 To 8) As Integer, i As Integer, c As Integer a(1) = 1 : a(2) = 2 : a(3) = 3 : a(4)=4 : a(5) = 7 : a(6) = 8∶a(7) = 15: a(8) = 25c=0For i = 3 To 8 If a(i - 2) + a(i - 1) = a(i) Then c = c + 1 Next iText1. Text = Str(c)該程序段運(yùn)行后,文本框Text1中顯示的內(nèi)容是( )A.1 B.2 C.3 D.42.有如下VB程序段:a(1) = 1a(2) = 3 For i = 3 To 10 a(i) = 2 * a(i - 1) - a(i - 2) Next i執(zhí)行該程序段后,數(shù)組元素a(8)的值為( )A.11 B.13 C.15 D.173.有如下VB程序段:a(1) = 1: a(2) = 2 b(1) = 1: b(2) = 2 For i = 3 To 10 a(i) = a(i - 1) + a(i - 2) b(i) = b(i - 1) + a(i) Next i執(zhí)行該程序段后,數(shù)組元素b(5)的值為( )A.5 B.10 C.18 D.314.有如下VB程序段: For i = 2 To 5 j = i k = a(j) Do While a(j - 1) < k And j > 1 a(j) = a(j - 1) j = j - 1 Loop a(j) = k Next i數(shù)組元素a(1)~a(5)的值依次為“79,20,34,56,61”,該程序段運(yùn)行后,數(shù)組元素a(1)~a(5)的值依次是( )A.79,56,34,20,61 B.79,61,56,34,20C.20,34,56,61,79 D.20,34,56,79,615.有如下VB程序段: s = “ ” For i = 1 To 5For j = 1 To a(i)s = s + Str(i)Next j Next i Label1.Caption = s數(shù)組元素a(1)~a(5)的值依次為“2,0,3,2,0”,執(zhí)行該程序段,標(biāo)簽Label1中顯示的內(nèi)容為( )A.1 2 1 2 3 1 2 B.1 1 3 3 3 4 4C.2 2 3 3 3 2 2 D.1 1 0 3 3 3 4 4 06.有如下程序段:Private Sub Command1_Click() Dim d(1 To 10) As Integer, f(1 To 10) As Boolean Dim i As Integer, ans As Integer ′給數(shù)組元素d(1)~d(10)賦值,代碼略 For i = 1 To 10f(i) = False Next i For i = 2 To 10If d(i) - d(i - 1) > 0 Then f(i) = True d(i) = d(i) - d(i - 1)End If Next i For i = 1 To 10If f(i) Then ans = ans + 1 Next i Label1.Caption = Str(ans)End Sub程序運(yùn)行時(shí),數(shù)組元素d(1)~d(10)的賦值分別為:25、15、10、8、1、7、8、11、16、5,則標(biāo)簽Label1中顯示的內(nèi)容為( )A.3 B.4 C.5 D.67.實(shí)現(xiàn)某算法的部分VB 程序段如下:i = 1Do While i <= 9If a(i) <> 0 Thenj = 10Do While j > i If a(j) = a(i) Then a(j) = 0 j = j - 1LoopEnd Ifi = i + 1LoopFor i = 1 To 10If a(i) <> 0 Then Text1.Text= Text1.Text+Str(a(i))Next i數(shù)組元素 a(1)到a(10)的數(shù)據(jù)依次為“4,1,6,4,4,9,1,7,6”,則程序運(yùn)行后,文本框Text1中顯示的內(nèi)容是( )A.4 9 1 7 6 B.4 1 6 9 7C.6 7 1 9 4 D.7 9 6 1 4二、非選擇題8.楊輝三角,又稱賈憲三角形,帕斯卡三角形,是二項(xiàng)式系數(shù)在三角形中的一種幾何排列。小楊同學(xué)設(shè)計(jì)了以下程序用來輸出楊輝三角,程序界面如下圖所示。程序運(yùn)行時(shí),在文本框Text1里輸入需要輸出的楊輝三角的行數(shù)n,單擊“打印”按鈕后,在列表框List1中輸出n行的楊輝三角。(1)要使程序運(yùn)行時(shí)自動(dòng)清空文本框Text1中的內(nèi)容,則應(yīng)在事件處理過程________(填字母:A.Form_Load/B.Text1_Click/C.Command1_Click)中編寫相應(yīng)代碼。(2)實(shí)現(xiàn)上述功能的VB程序如下,請?jiān)趧澗€處填入適當(dāng)?shù)拇a。Private Sub Command1_Click() Dim n As Integer, i As Integer, j As Integer, a(0 To 30) As Long Dim s As String List1.Clear ____①____ a(1) = 1 List1.AddItem Str(a(1)) For i = 2 To n a(1) = 1 s = “ ” For j = i To 1 Step -1 ____②____ s = Str(a(j)) + “ ” + s Next j ____③____ Next iEnd Sub9.小明編寫了一個(gè)特殊的排序程序,功能如下:隨機(jī)產(chǎn)生10個(gè)1~100之間的整數(shù),存儲(chǔ)在數(shù)組a并顯示在列表框 List1 中,單擊“排序”按鈕 Command1 后,在列表框 List2 中顯示降序排序后的結(jié)果,運(yùn)行界面如圖所示。特殊排序算法描述:(1)排序過程中不改變數(shù)據(jù)的位置,即保持原有數(shù)據(jù)的位置不變。(2)引入數(shù)組 index,用數(shù)組元素index(i)記錄第i大的數(shù)據(jù)在數(shù)列中的位置。(3)在數(shù)組 a 中找出最大數(shù)據(jù),將其所在位置存放在數(shù)組元素 index(1)中,然后在余下的數(shù)據(jù)中找出最大數(shù)據(jù),將其所在位置存放在index(2)中,以此類推,直到完成所有數(shù)據(jù)的位置存放。排序完畢, a(index(i))成為降序序列, 即 a(index(1)) ≥ a(index(2))≥a(index(3)) ≥…… ≥a(index(n))。如 n=5 時(shí),數(shù)組 a 排序后各變量值如下表所示。i12345a(i)17199136index(i)21435a(index(i))191713196實(shí)現(xiàn)上述功能的 VB 程序如下,在程序劃線處填入合適的代碼。Const n = 10, maxn = 100Dim a(1 To n) As Integer, index(1 To n) As Integer, flag(1 To n) As BooleanPrivate Sub Form_Load() Dim i As Integer Randomize For i = 1 To nflag(i) = Falsea(i) = Int(Rnd() * maxn) + 1List1.AddItem Str(a(i)) Next iEnd SubPrivate Sub Command1_Click() Dim i As Integer, j As Integer, k As Integer For i = 1 To n For j = 1 To n If flag(j) = False Then______①____:Exit For Next j For m = k + 1 To n If ____②____Then k = m Next m index(i) = k flag(k) = True Next i For i = 1 To n List2.AddItem Str(____③____) Next iEnd Sub10.小華有k個(gè)好朋友(編號為1~k),有一天,她的k個(gè)朋友向她要照片,可她手上只有n張照片(n(1)她根據(jù)和每個(gè)朋友的親密關(guān)系依次給他們打分;(分值為1~20間的整數(shù))(2)將k個(gè)朋友的打分按照分?jǐn)?shù)的個(gè)位數(shù)數(shù)字分成10組(0~9),再給每組的朋友生成一個(gè)隨機(jī)附加分;(分值為1~10間的整數(shù))(3)每個(gè)朋友的總分為親密關(guān)系分加上附加分,總分最高的前n個(gè)朋友將得到照片,若分?jǐn)?shù)相同,則編號小的朋友優(yōu)先。程序運(yùn)行時(shí),在文本框Text1中輸入照片數(shù),在文本框Text2中輸入朋友數(shù),單擊“讀取數(shù)據(jù)”按鈕Command1,將讀取與每個(gè)朋友的親密關(guān)系分和每組的附加分,并將親密關(guān)系分和附加分分別存放在數(shù)組a和數(shù)組c中,最后在列表框List1和List2中顯示;單擊“分組”按鈕Command2,將親密關(guān)系分進(jìn)行分組,分組情況顯示在列表框List3;單擊“分發(fā)”按鈕Command3,將分發(fā)情況(分到照片的朋友編號和總分)顯示在列表框List4中。程序運(yùn)行效果如下圖所示。小利依據(jù)上述描述設(shè)計(jì)了如下VB程序。請回答下列問題:(1)程序運(yùn)行時(shí),若在文本框Text2中輸入8,讀取的數(shù)據(jù)如圖所示,則第8位得到照片的朋友編號為________。(2)請?jiān)趧澗€處填入合適的代碼。Dim n As Integer, k As IntegerDim a(1 To 200) As Integer, b(0 To 9) As String, c(0 To 9) As IntegerDim d(1 To 10) As Integer, id(1 To 100) As Integer ′數(shù)組d存放總分,id存放朋友的編號Private Sub Command1_Click() ′讀取數(shù)據(jù)并存放在相應(yīng)的數(shù)組中,代碼略End SubPrivate Sub Command2_Click() Dim i As Integer, m As Integer, s As String For i = 1 To n m = ____①____ d(i) = ____②____ b(m) = b(m) + Str(a(i)) Next i For i = 0 To 9 If b(i) < > “ ” Then List3.AddItem Str(i) + “:” + b(i) Next iEnd SubPrivate Sub Command3_Click() Dim i As Integer, j As Integer, temp As Integer For i = 1 To n - 1For j = n To i + 1 Step -1 If ____③____ Thentemp = d(j): d(j) = d(j - 1): d(j - 1) = temptemp = id(j): id(j) = id(j - 1): id(j - 1) = temp End IfNext j Next i For i = 1 To k List4.AddItem Str(id(i)) + “ 號朋友的總分 ” + Str(d(i)) Next iEnd Sub11.小李與小王合作編寫一個(gè)成績統(tǒng)計(jì)的VB程序:小王編寫一個(gè)過程,該過程從數(shù)據(jù)庫讀取某一指定科目的相關(guān)數(shù)據(jù),存儲(chǔ)在數(shù)組a中;小李編寫一個(gè)過程,該過程依據(jù)數(shù)組a中的相關(guān)數(shù)據(jù)統(tǒng)計(jì)各班平均分。小李與小王約定的數(shù)組a各元素含義如表所示。數(shù)組元素數(shù)組元素的含義a(1)存儲(chǔ)班級數(shù)na(2)從a(2)到a(n+1) 依次存儲(chǔ)第1、2、…第n個(gè)班級人數(shù)…a(n+1)a(n+2)從a(n+2) 依次存儲(chǔ)第1班每個(gè)學(xué)生的單科成績、第2班每個(gè)學(xué)生的單科成績、…第n班每個(gè)學(xué)生的單科成績……程序功能如下:在文本框Text1中輸入科目名稱,單擊“讀取數(shù)據(jù)庫”按鈕Command1,程序從數(shù)據(jù)庫讀取數(shù)據(jù);單擊“開始統(tǒng)計(jì)”按鈕Command2,程序進(jìn)行統(tǒng)計(jì)處理,結(jié)果輸出在列表框List1中。程序運(yùn)行界面如下圖所示。實(shí)現(xiàn)上述功能的VB程序如下,請回答下列問題:(1)根據(jù)程序運(yùn)行界面中的數(shù)據(jù)及數(shù)組a各元素的含義,數(shù)組元素a(5)的值為________(填寫數(shù)值)。(2)分析程序,可知數(shù)據(jù)庫的文件名為________。(3)請?jiān)趧澗€處填入合適的代碼。Dim a(1 To 600) As Integer ′數(shù)組大小滿足處理要求Private Sub Command1_Click() ′本過程由小王完成,從數(shù)據(jù)庫讀取指定科目的各相關(guān)數(shù)據(jù),存儲(chǔ)在數(shù)組a中 Dim conn As New ADODB.Conncction Dim rs As New ADODB.Recordset conn ConncctionString=“provider=Microsoft.ACE.OLEDB.12.0;data source=”+ “Score.accdb” conn.Open Set rs.ActiveConnection = conn ′本過程的其他語句略End SubPrivate Sub Command2_Click() ′依據(jù)數(shù)組a中的相關(guān)數(shù)據(jù)統(tǒng)計(jì)各班級平均分 Dim i As Integer, j As Integer, n As Integer Dim p As Integer, sum As Integer, aver As Single ________①________ p = n + 2 For i = 1 To n sum = 0 For j = 1 To a(i + 1) ________②________p = p + 1 Next j aver = sum / a(i + 1) List1.AddItem Str(i) + “ ” + Str(a(i + 1)) + “ ” + Str(aver) Next iEnd Sub12.由數(shù)組a生成數(shù)組b的方法描述如下:(1)將數(shù)組a中的n個(gè)元素依次分割出若干個(gè)數(shù)據(jù)塊,每個(gè)數(shù)據(jù)塊有m×m個(gè)元素,m最大值為8,最小值為2。分割時(shí),按盡可能大的數(shù)據(jù)塊進(jìn)行分割;(2)對每個(gè)分割出的數(shù)據(jù)塊用“方陣轉(zhuǎn)換法”進(jìn)行轉(zhuǎn)換,每次轉(zhuǎn)換后得到的數(shù)據(jù)塊依次存儲(chǔ)在數(shù)據(jù)b中;(3)數(shù)組a分割后的剩余元素(個(gè)數(shù)小于4),直接依序存儲(chǔ)到數(shù)組b中。例如n=140時(shí),可依次分割出3個(gè)數(shù)據(jù)塊,元素的個(gè)數(shù)分別為64(8×8)、64(8×8)、9(3×3),剩余元素為3個(gè)。“方陣轉(zhuǎn)換法”過程如下:將數(shù)據(jù)塊中m×m個(gè)元素按行序排列成一個(gè)數(shù)字方陣,從該數(shù)字方陣中按列序得到轉(zhuǎn)換后元素的次序。以3×3數(shù)據(jù)塊為例,轉(zhuǎn)換過程如下圖所示:← 轉(zhuǎn)換前元素次序轉(zhuǎn)換后元素次序→小明依據(jù)上述描述設(shè)計(jì)了如下VB程序。請回答下列問題:(1)當(dāng)n=120時(shí),分割出的第3個(gè)數(shù)據(jù)塊元素個(gè)數(shù)為________。(2)請?jiān)趧澗€處填入合適的代碼。Const n = 120Dim a(1 To n) As IntegerDim b(1 To n) As IntegerPrivate Sub Command1_Click() Dim m As Integer, i As Integer Dim Start As Integer ′當(dāng)前未分割數(shù)據(jù)的第1個(gè)元素下標(biāo) Dim Left As Integer ′當(dāng)前未分割數(shù)據(jù)的個(gè)數(shù) Dim pa As Integer ′數(shù)組a的下標(biāo) Dim pb As Integer ′數(shù)組b的下標(biāo) ′讀取n個(gè)轉(zhuǎn)換前的數(shù)據(jù),依次存儲(chǔ)到a(1)、a(2)、……a(n)中,代碼略 m = 8 Start = 1 Left = n Do While Left > 3 If Left < m * m Thenm = ______①______ Else pa = Start pb = Start For i = 1 To m * m b(pb) = a(pa) pb = pb + 1 If i Mod m = 0 Then ______②______ Else pa = pa + m End If Next i ______③______ Start = Start + m * m End If Loop For i = Start To nb(i) = a(i) Next i′依次輸出轉(zhuǎn)換后數(shù)據(jù)b(1)、b(2)、……b(n),代碼略End Sub13.小王編寫了一個(gè)VB程序,該程序的功能是:有15個(gè)數(shù)形成環(huán)狀,現(xiàn)要分別找出3個(gè)相鄰的數(shù),使其相加之和最大或最小。如15個(gè)數(shù)依次為:18 14 4 26 1 13 19 14 13 28 5 26 15 8 30,則相鄰三數(shù)之和最大為62(30+18+14),相鄰三數(shù)之和最小為31(4+26+1)。程序運(yùn)行時(shí),先隨機(jī)生成15個(gè)[1,30]區(qū)間內(nèi)的整數(shù),存儲(chǔ)在數(shù)組a中,并在文本框Text1中顯示;單擊“計(jì)算”按鈕Command2,則在標(biāo)簽Label4中顯示連續(xù)三數(shù)最大和,在標(biāo)簽Label5中顯示連續(xù)三數(shù)最小和,程序運(yùn)行界面如圖所示。實(shí)現(xiàn)上述功能的VB程序如下,請回答下列問題:(1)若15個(gè)數(shù)分別為:30 8 26 7 5 8 16 16 21 18 24 30 26 27 30,則相鄰三數(shù)之和最大最小值分別為____________________。(2)請?jiān)趧澗€處填入合適的代碼。Dim a(0 To 14) As IntegerPrivate Sub Form_Load() ′隨機(jī)生成15個(gè)數(shù),存儲(chǔ)在數(shù)組元素a(0)~a(14)中,并顯示在文本框Text1中End SubPrivate Sub Command1_Click() Dim i As Integer, j As Integer, k As Integer, max As Integer, min As Integer Dim sum As Integer, maxi As Integer, mini As Integer sum = 0 max = 0 min = 5000 For i = 0 To 14 j = (i + 1) Mod 15 ______①______ sum = a(i) + a(j) + a(k) If sum > max Then max = sum ______②______ End If If sum < min Then min = sum mini = i End IfNext iLabel4.Caption=a(maxi)&“+”&a((maxi+1) Mod 15)&“+”&a((maxi+2) Mod 15)& “=”&maxLabel5.Caption=a(mini)&“+”&a((mini+1) Mod 15)&“+”&a((mini+2) Mod 15)& “=” &minEnd Sub14.產(chǎn)生10個(gè)[20-30]之間的不重復(fù)的正整數(shù),并顯示在列表框List1中給20~30號數(shù)組元素編號。如果數(shù)組元素為0,表示未產(chǎn)生,并把該數(shù)組元素的標(biāo)志修改為1,直到產(chǎn)生10個(gè)數(shù)為止。例如產(chǎn)生的隨機(jī)數(shù)是21,由先判斷S(21)的值,如果大于0,表示該元素已經(jīng)產(chǎn)生,如果s(21)=0表示該元素未產(chǎn)生,把該數(shù)產(chǎn)生的順序存儲(chǔ)。Private Sub Command1_Click() Dim s(20 to 30) As Integer For i = 20 To 30 s(i) = 0 Next i Randomize i = 1 Do While i <= 10______①________List2.AddItem Str((x)) ′顯示程序過程中產(chǎn)生的數(shù)If s(x) = 0 Then ______②________ i =i+1 End If Loop For i =20 to 30 If s(i) > 0 Then List1.AddItem Str(i) + “產(chǎn)生順序:” + Str(s(i)) Next iEnd Sub 展開更多...... 收起↑ 資源列表 專項(xiàng)測試七 數(shù)據(jù)存儲(chǔ)與數(shù)組應(yīng)用.doc 專項(xiàng)測試七 數(shù)據(jù)存儲(chǔ)與數(shù)組應(yīng)用原卷版.doc 縮略圖、資源來源于二一教育資源庫