(完整word版)江蘇省計(jì)算機(jī)等級(jí)考試二級(jí)VB常用算法_第1頁(yè)
(完整word版)江蘇省計(jì)算機(jī)等級(jí)考試二級(jí)VB常用算法_第2頁(yè)
(完整word版)江蘇省計(jì)算機(jī)等級(jí)考試二級(jí)VB常用算法_第3頁(yè)
(完整word版)江蘇省計(jì)算機(jī)等級(jí)考試二級(jí)VB常用算法_第4頁(yè)
(完整word版)江蘇省計(jì)算機(jī)等級(jí)考試二級(jí)VB常用算法_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、(1)累加和連乘1、算法說(shuō)明累加形式:V=V+e連乘形式:V=V*e其中:V是變量,e是遞增表達(dá)式。累加和連乘一般通過(guò)循環(huán)結(jié)構(gòu)來(lái)實(shí)現(xiàn)。注意:需在執(zhí)行循環(huán)體前對(duì)變量 V賦初值。一般的,累加時(shí)置初值 0;連乘時(shí)置初值為1.舉例求N!的結(jié)果。Private Sub Command1_Click ()Dim n%, i%, s&n = Val (InputBox ("輸入 n") s = 1For i = 1 To ns = s * iNext iPrint sEnd Sub錯(cuò)誤的寫(xiě)法:Private Sub Command1_Click ()Dim n%, i%, s&a

2、mp;n = Val (InputBox ("輸入 n")For i = 1 To ns = 1賦初值語(yǔ)句位置不對(duì)!s = s * iNext iPrint s輸出s的值為n,而不是n!End Sub應(yīng)用舉例根據(jù)下列公式,求自然對(duì)數(shù)e的的近似值。要求:誤差小于0.00001Private Sub Command1_Click () Dim i% , n&, t! , e! e = 2 i = 1 t = 1Do While t > 0.00001 i = i + 1 t = t / i e = e + t LoopPrint "計(jì)算了";

3、i;"項(xiàng)目和是:";ePrint Exp (1)與上旬輸出值進(jìn)行對(duì)比以證明算法的正確性End Sub解題技巧1)由于這類題目往往是根據(jù)精度要求來(lái)求值,因此我們不能預(yù)知具體循環(huán)次數(shù),所以這類題目一般用Do循環(huán),很少用For循環(huán)。設(shè)定循環(huán)變量和通項(xiàng)變量,注意各變量的初 值;2)分解通項(xiàng)表達(dá)式中各因子,并分別將各因子用循環(huán)變量表示;3)如果步驟2中有的因子比較復(fù)雜,難以直接用變量表示,此時(shí)可以考慮使用 Function 過(guò)程;4)根據(jù)步驟1、2、3,寫(xiě)出通項(xiàng)表達(dá)式;5)根據(jù)精度要求(往往是通項(xiàng)小于10負(fù)多少次方這樣一個(gè)關(guān)系表達(dá)式),寫(xiě)出一 條滿足精度要求后跳出循環(huán)的語(yǔ)句。通常是用

4、:if通項(xiàng)表達(dá)式10A (-N) then exit do ,注 意這句話一般需放在累加或者連乘式之前。實(shí)例說(shuō)明以2002年春上機(jī)試卷06編程題為例根據(jù)X值計(jì)算:oK -+9上一2!42(n-l)rn=1, 2,要求:n項(xiàng)絕對(duì)值小于等于10-6為止。1、由于循環(huán)次數(shù)不確定,因此確定用 Do循環(huán)結(jié)構(gòu)并定義循環(huán)變量用n表示(初值 1);用戶輸入的信用x表示;通項(xiàng)用dblCos表示;累加值用sum表示,初值為0;2、分解通項(xiàng)式的組成(_嚴(yán)2(n-l)!可以分解為三部分:(-1嚴(yán)+可以表小為:(一1) a (n+1)JEF 一、,)可以表小為:xA (2* (n-1)2-1)!比較復(fù)雜,難以直接表示3

5、、由于步驟2中DA復(fù)雜,此時(shí)考慮使用過(guò)程。于是定義過(guò)程,輸入值是 于是有:n,返回值是2(7/-1)!private Function comp (n as long) as longdim I as longdim result as longresult=1 此處注意,由于是連乘,初值為 1for I=1 to 2* (n-1)result=result*Inext I comp=resultEnd Function注意:由于是參數(shù)按地址傳遞,因此對(duì)于本題,實(shí)參的值不能在過(guò)程中被改變!(也是改錯(cuò)題常考的地方! !)4、根據(jù)步驟1、2、3,寫(xiě)出通項(xiàng)dblCos的表達(dá)式:dblCos= (1)

6、 A (n+1) * xA (2* (n-1) ) /comp (n)5、根據(jù)精度要求知:If abs (dblCos) <=10a (-6) then exit do最后程序?yàn)椋篜rivate Sub Command1_Click ()Dim n As Long , dblCos As Double, x As Double x = Val (Textl.Text) n = 1 DodblCos = (-1) a 5 + 1) * x a (2 * (n - 1) / comp (n)If Abs (dblCos) <= 10 a (-6) Then Exit Do Sum =

7、Sum + dblCos n = n + 1LoopPrint Sum End SubPrivate Function comp (n As Long) As LongDim I As LongDim result As Longresult = 1 '此處注意,由于是連乘,初值為 1For I = 1 To 2 * (n - 1) result = result * INext I comp = result End Function注意:如果調(diào)試運(yùn)行時(shí)死循環(huán),可以按 Ctrl + Break中斷死循環(huán),不需要重新啟動(dòng)機(jī) 器。(或者 Ctrl+Scroll Lock )2、實(shí)戰(zhàn)練習(xí)1

8、)補(bǔ)充代碼(2003秋二(7) 本程序的功能是求下面數(shù)列前 n項(xiàng)之和。S (x, n) =x/2+2! *xA3/2*4+3 ! *xA5/2*4*6+ +n! *xA2n-1/2*4*6 2n Option ExplicitPrivate Sub Command1_Click ()Dim x As Single , s As SingleDim n As Integer, i As Integerx=InputBox ("輸入X:","求數(shù)列和u , 1)n=InputBox ("輸入N:","求數(shù)列和",1) For i

9、=1 To ns=s+fun (x, i)Next iLabel1.Caption= (1)Text1=sEnd SubPrivate Function fun (x As Single, n As Integer) As Single(2)p=1For i=1 To n(3) Next ifun=xA (2*n-1) *pEnd Function2)編程題( 2003春上機(jī)試卷03)按下列公式編寫(xiě)求積分余弦函數(shù)值的程序。當(dāng)通項(xiàng)絕對(duì)值小于10- 5時(shí)停止計(jì)算。f(")= in、+E(-i /2瓶町! 0a(2)最值問(wèn)題1、算法說(shuō)明在若干數(shù)中求最大值,一般先取第一個(gè)數(shù)為最大值的初值(即

10、假設(shè)第一個(gè)數(shù)為最大值),然 后,在循環(huán)體內(nèi)將每一個(gè)數(shù)與最大值比較,若該數(shù)大于最大值,將該數(shù)替換為最大值,直到 循環(huán)結(jié)束。求最小值的方法類同。求若干數(shù)平均值,實(shí)質(zhì)上就是先求和,再除以這些數(shù)的個(gè)數(shù)。應(yīng)用舉例隨機(jī)產(chǎn)生n個(gè)1 100 (包括1和100)的數(shù),求它們的最大值、最小值和平均值。Private Sub Command1_Click()Dim n As Integer, i As Integer, min As Integer, max As Integer, aver As Single, s As Integer n = Val(InputBox("輸入個(gè)數(shù):")s =

11、 Int(Rnd * 100) + 1max = smin = saver = sPrint "第1個(gè)數(shù)是:"& sFor i = 2 To ns = Int(Rnd * 100) + 1Print "第"& i & "個(gè)數(shù)是:"& sIf s > max Then max = sIf s < min Then min = saver = aver + sNext iaver = aver/nPrint "max=";max; "min=" min;

12、"aver=";averEnd Sub解題技巧最大值、最小值、平均值類型題目往往和數(shù)組放在一起考!有的不僅求這些值,還要對(duì)具有 最大值或者最小值的行或列或者某個(gè)元素進(jìn)行處理,這時(shí)就要在記錄最大、最小值時(shí),同時(shí) 記錄該值所在的行號(hào)和列號(hào)。2、實(shí)戰(zhàn)練習(xí)1) 補(bǔ)充代碼(2000春二(9)本程序的功能是在二維數(shù)組中查找鞍點(diǎn)元素,即該元素在所在行中為最大,且在所在列 中為最小。在一個(gè)數(shù)組中可能存在,也可能不存在這樣的元素。數(shù)組各元素的值從文件data.txt 中讀取。Private Sub Form_Click()Dim a(3,3) As IntegerAs Integer,j A

13、s IntegerDim maxvr As Integer,col As Integer, As IntegerOpen data.txt For Input As #1For i=1 To 3For j=1 To 3Input #1,a(i,j)Print a(i,j);Next jPrintNext iFor i=1 To 3maxvr= (1)col=1For j=2 To 3If maxvrmaxvr= (2)col=jEnd IfNext jFor j=1 To 3If maxvr>a(j,col) Then (3)Next jEnd sub(3)素?cái)?shù)1、算法說(shuō)明素?cái)?shù)(質(zhì)數(shù)):

14、就是一個(gè)大于等于2的整數(shù),并且只能被1和本身整除,而不能被其他整數(shù) 整除的數(shù)。判別某數(shù)m是否是素?cái)?shù)的經(jīng)典算法是:對(duì)于m,從I = 2, 3, 4,,m-1依次判別能否被I整除,只要有一個(gè)能整除,m就不 是素?cái)?shù),否則m是素?cái)?shù)。Private Function sushu(ByVal n As Long) As BooleanDim i As LongFor i = 2 To n - 1If (n Mod i) = 0 Then Exit ForNext IIf I=n then sushu=TrueEnd Function很顯然,實(shí)際上,我們可以改進(jìn)上面For i = 2 To n -1為:Fo

15、r i = 2 To int (sqr (m) 這樣可以很好的提高效率。 以上判斷是否為素?cái)?shù)的代碼務(wù)必識(shí)記! 應(yīng)用舉例求100200之內(nèi)素?cái)?shù)。Private Sub Command1_Click() Dim j As Integer For j = 100 To 200If sushu(j) = True Then Print jEnd IfNext jEnd Sub解題技巧識(shí)記判斷素?cái)?shù)的算法過(guò)程,根據(jù)題意,靈活調(diào)用!實(shí)例說(shuō)明編程題(2002年春上機(jī)試卷04)找出10000以內(nèi)所有可以表示為兩個(gè)平方數(shù)和的素?cái)?shù)。思路:首先找10000以內(nèi)的所有素?cái)?shù),對(duì)于每個(gè)素?cái)?shù)判斷其是否可以表示為兩個(gè)平方數(shù)之和

16、(即對(duì)于任意小于該素?cái)?shù)shu的數(shù)I,如果I和shuI均為平方數(shù),則說(shuō)明其可以表示為兩個(gè)平方 數(shù)之和。)判斷數(shù)I是否為平方數(shù)的方法:sqr=int(sqr(i)Private Sub Command1_Click()Dim j As IntegerDim m As Long, n As LongFor j = 2 To 10000If sushu(j) = True ThenIf pf(j, m, n) = True ThenList1.AddItem j & "=" & m & "+" & n End IfEnd IfNe

17、xt jEnd SubPrivate Function pf(ByVal shu As Long, m As Long, n As Long) As Boolean Dim i As Long For i = 1 To shu - 1If (Sqr(i) = Int(Sqr(i) And (Sqr(shu - i) = Int(Sqr(shu - i) Then pf = True m = i n = shu - i Exit FunctionEnd IfNext End Function 2、實(shí)戰(zhàn)練習(xí)1) 補(bǔ)充代碼(2002春二(7)下列程序的功能是:查找四位正整數(shù)中的超級(jí)素?cái)?shù)。超級(jí)素?cái)?shù)的定

18、義為:當(dāng)一個(gè)素?cái)?shù)從低位 到高位依次去掉一位數(shù)后剩下的數(shù)仍然是素?cái)?shù),則此數(shù)為超級(jí)素?cái)?shù)。如數(shù)2333、233、23、2均為素?cái)?shù),所以2333為超級(jí)素?cái)?shù)。Option ExplicitPrivate Sub Command1_Click()Dim I As Integer, flg As BooleanFor I = 1001 To 9999 Step 2Call sup_prime(I, flg)If flg ThenDebug.Print IEnd IfNext IEnd SubPrivate Sub sup prime( (1), F As Boolean)Dim p As Integer F

19、 = True Do While N > 0 If prime(N) Then (2) Else (3) Exit Sub End If Loop End SubPublic Function prime(p As Integer) As BooleanDim k As IntegerIf p = 1 ThenExit Function Else For k = 2 To Sqr(p) If p Mod k = 0 Then Exit Function Next k (4)End If End Function2) 編程題(2004春上機(jī)試卷03)隨機(jī)生成15個(gè)兩位正整數(shù),從中找出所有的

20、素?cái)?shù),并記下它是第幾個(gè)數(shù),再找出其中 最大的素?cái)?shù),并給出它的位置。廝找出最大案數(shù)及其序號(hào) 口目區(qū)皿 11 81 C5 49 49 29 30 1S 22 U «3 M11 2.大索皴:E5-罅一0J83- 13廳號(hào):13選藥'I(4)進(jìn)制轉(zhuǎn)化1、算法說(shuō)明1) 十進(jìn)制正整數(shù)m轉(zhuǎn)換為R (216)進(jìn)制的字符串。思路:將m不斷除r取余數(shù),直到商為0,將余數(shù)反序即得到結(jié)果。算法實(shí)現(xiàn):Private Function Tran(ByVal m As Integer, ByVal r As Integer) As StringDim StrDtoR As String, n As Int

21、egerDo While m <> on = m Mod rm = m rIf n > 9 ThenStrDtoR = Chr(65 + n - 10) & StrDtoRElseStrDtoR = n & StrDtoREnd IfLoopTran = StrDtoREnd Function2) R (216)進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制正整數(shù)。思路:R進(jìn)制數(shù)每位數(shù)字乘以權(quán)值之和即為十進(jìn)制數(shù)。算法實(shí)現(xiàn):Private Function Tran(ByVal s As String, ByVal r As Integer) As integerDim n As In

22、teger, dec As Integers = UCase(Trim(s)For i% = 1 To Len(s)If Mid(s, i, 1) >= "A" Thenn = Asc(Mid(s, i, 1) - Asc("A") + 10Elsen = Val(Mid(s, i, 1)End Ifdec = dec + n * r A (Len(s) - i)Next iTran = decEnd Function解題技巧進(jìn)制轉(zhuǎn)化的原理要清楚,同時(shí)編寫(xiě)代碼時(shí)候要留意16進(jìn)制中的A F字符的處理 2、實(shí)戰(zhàn)練習(xí)1)補(bǔ)充代碼(2002秋二(9)本程序

23、是把給定的二進(jìn)制整數(shù)轉(zhuǎn)換為八進(jìn)制整數(shù)。Private Sub Command1_Click()Dim a As String, b As String, c As StringDim L As Integer, m As Integer, n As Integera = InputBox("請(qǐng)輸入一個(gè)二進(jìn)制數(shù)","輸入框")(1)a = String(L, "0") & a(2)For m = 1 To n / 3b = Mid(a, 3 * m - 2, 3)(3)Next mTextl.Text = cEnd SubPri

24、vate Function zh(s As String) As StringDim i As Integer, n As Integer, p As Integerp = 1For i = 2 To 0 Step -1(4) (5) = p + 1Next izh = Str(n)End Function2)補(bǔ)充代碼(2001春二(7)下面程序是把給定的16進(jìn)制正整數(shù)轉(zhuǎn)換為10進(jìn)制數(shù)。Option ExplicitPrivate Sub Form_Click()Dim St As Integer, Dem As LongSt=InputBox("輸入一個(gè)十六進(jìn)制數(shù)")De

25、m=Convert(St)Print St; ">=" DemEnd SubPrivate Function Convert(S As String)As LongDim N As Integer, I As Integer, Substring As String*1Dim P As long, K As Long , Asc1 As IntegerN= (1)P=16ANFor I=1 To NP=P/16Substring= Select Case SubstringCase "0" To "9"K=K+P*Val(Sub

26、string)Case(3)Asc1=Asc(Substring)-Asc("A")+10(4) End SelectNext I(5)End Sub(5)約數(shù)因子1、算法說(shuō)明1)最大公約數(shù):用輾轉(zhuǎn)相除法求兩自然數(shù) m、n的最大公約數(shù)。(1) 首先,對(duì)于已知兩數(shù) m、n,比較并使得m>n;(2) m除以n得余數(shù)r;(3) 若r=0,則n為求得的最大公約數(shù),算法結(jié)束;否則執(zhí)行步驟(4)(4) m?n n?r再重復(fù)執(zhí)行(2)譬如:10與5分析步驟:m=10 n=5r=m mod n=0所以n(n=5)為最大公約數(shù)24與9分析步驟:m=24 n=9r=m mod n=6 r

27、 w 0 m=9 n=6 r=m mod n=3 r w 0 m=6 n=3 r=m mod n=0 所以n(n=3)為最大公約數(shù)算法實(shí)現(xiàn)循環(huán)實(shí)現(xiàn)Private Function GCD(ByVal m As Long, ByVal n As Long) As LongDim temp As LongIf m < n Then temp = m: m = n: n = tempDim r As LongDor = m Mod nIf r = 0 Then Exit Dom = nn = rLoopGCD = nEnd Function遞歸實(shí)現(xiàn)Private Function GCD(By

28、Val m As Long, ByVal n As Long) As LongDim temp As LongIf m < n Then temp = m: m = n: n = tempDim r As Longr = m Mod nIf r = 0 ThenGCD = nElsem = nn = rGCD = GCD(m, n)End IfEnd Function2) 最小公倍數(shù)mx n或大公約數(shù)3) 互質(zhì)數(shù)最大公約數(shù)為1的兩個(gè)正整數(shù) 解題技巧該算法需要識(shí)記!這種類型題目的擴(kuò)展是約數(shù)和因子題型。4) 實(shí)戰(zhàn)練習(xí)1) 補(bǔ)充代碼(2003春二(9)給定一個(gè)十進(jìn)制正整數(shù),找出小于它并與其互質(zhì)

29、的所有正整數(shù)(所謂互質(zhì)數(shù)是指最大 公約數(shù)為1的兩個(gè)正整數(shù),下圖是程序執(zhí)行畫(huà)面)。Option Explicit(1) As IntegerPrivate Function gcd ( Dim r As Integerr = m Mod nIf r = 0 Then gcd = nElse m = n: n = r(2)End IfEnd FunctionPrivate Sub Command1_Click()Dim n As Integer, p As Integern = Val(Text1)For p = n - 1 To 2 Step -1If(3)Then List1.AddItem

30、pNext p End Sub2) 編程題( 2002秋上機(jī)試卷01)生成一個(gè)三行八列的二維數(shù)組A(3,8),其中前兩行元素產(chǎn)生的方法是:用初值 X1=26 及公式 Xi+1=(25 >Xi+357) Mod 1024,產(chǎn)生一個(gè)數(shù)列:X1、X2、X16 其中X1X8作為A的第一行元素;X9X16作為A的第二行元素;A的第三行元素值取前 兩行同列元素的最大公約數(shù)。最后按圖示格式顯示在圖片框中。,數(shù)組生成回區(qū)數(shù)組口平生一261007956705547371 416 517994 631 772 201 262 763 1000 781清除214327811退出(6)排序1、算法說(shuō)明1) 選擇

31、法排序(1) 從n個(gè)數(shù)中選出最小數(shù)的下標(biāo),出了循環(huán),將最小數(shù)與第一個(gè)數(shù)交換位置;(2) 除第一個(gè)數(shù)外,在剩下的n-1個(gè)數(shù)中再按方法(1)選出次小的數(shù),與第二個(gè)數(shù)交換位置;(3) 以此類推,最后構(gòu)成遞增序列。譬如:869327第一輪交換后269387第一輪交換后239687第二輪交換后236987第四輪交換后236789第五輪無(wú)交換236789程序代碼如下:Private Sub xzPaiXu(a() As Double, sheng As Boolean)'a為需要排序的數(shù)組,sheng為True則為升序排列,為False,則為降序排列Dim i As Integer, j As I

32、ntegerDim temp As DoubleDim m As IntegerFor i = LBound(a) To UBound(a) - 1'進(jìn)行數(shù)組大小-1 輪比較m = i'在第i 輪比較時(shí),假定第'i 個(gè)元素為最值元素For j = i + 1 To UBound(a)'在剩下的元素中找出最'值元素的下標(biāo)并記錄在m 中If sheng Then'若為升序,則m 記錄最小元素'下標(biāo),否則記錄最大元素下標(biāo)If a(j) < a(m) Then m = jElseIf a(j) > a(m) Then m = jEnd

33、 IfNext j'將最值元素與第i 個(gè)元素交換temp = a(i)a(i) = a(m)a(m) = tempNext iEnd Sub調(diào)用該過(guò)程示例:Option Base 1Private Sub Command1_Click()Dim b(6) As Doubleb(1) = 8b(2) = 6b(3) = 9b(4) = 3b(5) = 2b(6) = 7Call xzPaiXu(b, True)For i% = 1 To 6Print b(i)NextEnd Sub2) 冒泡法排序選擇排序法在每一輪排序時(shí)找最值元素的下標(biāo),出了內(nèi)循環(huán)(一輪排序結(jié)束),再交換最小數(shù)的位置;而

34、冒泡法在每一輪排序時(shí)將相鄰的數(shù)比較,當(dāng)次序不對(duì)就交換位置,出了內(nèi)循環(huán),最值數(shù)已經(jīng)冒出。譬如:8 6 9 3 2 78693278692378629378269372869372 3 6 8 7 9 .2 3 6 7 8 9 .2 3 6 7 8 9程序代碼如下:Private Sub mpPaiXu(a() As Double, sheng As Boolean)'a為需要排序的數(shù)組,sheng為True則為升序排列,為False,則為降序排列Dim i As Integer, j As IntegerDim temp As DoubleDim m As IntegerFor i =

35、LBound(a) To UBound(a) - 1'進(jìn)行 n-1 輪比較For j = UBound(a) To i + 1 Step -1 '從n至U i個(gè)元素兩兩進(jìn)行比較If sheng Then'若次序不對(duì),馬上進(jìn)行交換If a(j) < a(j - 1) Thentemp = a(j)a(j) = a(j - 1)a(j - 1) = tempEnd IfElseIf a(j) > a(j - 1) Thentemp = a(j)a(j) = a(j - 1)a(j - 1) = tempEnd IfEnd IfNext j出了內(nèi)循環(huán),一輪排序結(jié)束

36、'最值元素冒到最上邊Next iEnd Sub調(diào)用該過(guò)程代碼基本同上。2、實(shí)戰(zhàn)練習(xí)1) 補(bǔ)充代碼(2003秋二(10)下面是一個(gè)采用拉鋸式排序法對(duì)數(shù)組元素按升序進(jìn)行排序的程序, 所謂拉鋸式排序法 是這一遍把最小的元素從下到上送到最上的位置,下一遍則是從上到下把最大的元素送到最 下的位置。Option Base 1Private Sub Command1_Click()Dim a(10) As Integer,i As IntegerFor i = 1 To 10a(i) = Int(Rnd * 10)+1Textl = Text1 & Str(a(i)Next iCall sh

37、aker_sort(a)For i = 1 To 10Text2 = Text2 & Str(a(i)Next iEnd SubPrivate Sub Shaker_sort(k() As Integer)Dim i As Integer,c As Integer,d As Integer Dim t As Integer c = 1 d =(1)DoFor Step-1 If k(i=1)>k(i) Then t = k(i-1):k(i-1) = k(i):k(i) = t End IfNext i For i = c+1 To dIf(4)Thent = k(i-1):k(

38、i-1) = k(i):k(i) = tEnd If Next i d = d-1Loop While (5) End Sub2) 編程題(2002秋上機(jī)試卷04)把文本框輸入的字符用按降序添加到列表框中(7)插入和刪除1、算法說(shuō)明數(shù)組中元素的插入和刪除一般是在已固定序列的數(shù)組中插入或刪除一個(gè)元素, 使得插入或刪除操作后的數(shù)組還是有序的?;舅悸罚菏紫纫业讲迦胛恢没蛞?jiǎng)h除的元素。1) 插入14一10131619222匹14,代碼如下:Private Sub Command1_Click()Dim a(10) As IntegerDim i As Integer, k As IntegerFo

39、r i = 0 To 9'生成數(shù)組a(i) = i * 3 + 1Print a(i);Next iPrintPrint "插入 14"For k = 0 To 9'查找插入14在數(shù)組中的位置If 14 < a(k) Then Exit ForNext kFor i = 9 To k Step -1 '從最后元素開(kāi)始逐個(gè)后移,騰出位置 a(i + 1) = a(i)Next ia(k) = 14'插入數(shù) 14For i = 0 To 10Print a(i);Next iPrintEnd Sub2) 147101316192225 28

40、K14-101316192225A刪除 代碼如下:Dim a() as integer .ReDim a(1 to n) For i=k+1 to na(i-1)=a(i) Next IRedim preserve a(1 to n-1)2、實(shí)戰(zhàn)練習(xí)1)補(bǔ)充代碼( 2001秋二(8)(此題文件部分實(shí)在看不懂的話可以不看:蒯)C盤根目錄下文件 Data4.txt的內(nèi)容是:2, 4, 6, 8, 10, 1, 3, 5, 7, 9。下面程序的功能是將文件后半部分的奇數(shù)分別按序插入到前半部分的適當(dāng)位置,得到的新數(shù)列是:1234) 5 6 7 8 9 1Q (實(shí)現(xiàn)方法:第一次調(diào)整后的數(shù)列是:1 2 4

41、 6 8 10 3 5 7 9第二次調(diào)整后的數(shù)列是:1 2 3 4 6 8 10 5 7 9 。Option ExplictPrivate Sub Form_Click()Dim A(10) As IntegerAs Integer,J As IntegerOpen "c:Data4.txt" For Input As #12 Do (1)J=J+1Input #12,A(J)LoopCall Insert(A)For i=1 To 10Print A(i);Next i Print Close #12 End Sub Private Sub Insert(A() As I

42、nteger)Dim i As Integer,Putp As Integer,J As IntegerDim Getp As Integer,N As Integer,Tem As Integer N=UBound(A)/2 Putp=1 Getp=N+1 For i=1 To N Tem=A(Getp) For J=Getp To Putp +1 Step -1 Next J A(Putp)=Tem Getp=Getp+1 Putp=(3)Next i End Sub2) 補(bǔ)充代碼(2001春二(8)下面程序得功能是將無(wú)序數(shù)組中相同得數(shù)只保留一個(gè),其余得刪除,并輸出經(jīng)過(guò)刪除 后的數(shù)組元素,

43、刪除相同數(shù)是通過(guò)將該數(shù)組元素后面的元素在數(shù)組內(nèi)依次前移替換前一個(gè)元 素的值實(shí)現(xiàn)的。數(shù)組各元素的值從文件 data.txt中讀取。Option ExplictOption Base 1Private Sub Form_Click()Dim I As Integer,J As Integer,K As Integer Dim A()As Integer,T As Integer,M As Integer Open "c:my documents2000testdata.txt" For Input As #1 Do While (1) I=I+1 Redim Preserve

44、A(I) Input #1,A(I) Loop M=1:T=(2)Do While M<TI=M+1 Do While I<=T If A(I)=A(M)Then For J=1 To(3)A(J)=A(J+1) Next J T=T-1 ElseI= (4)End IfLoopM=M+1LoopRedim Preserve A(T)For I=1 To TPrint A(I);Next IPrintEnd Sub(8)查找1、算法說(shuō)明1) 順序查找逐個(gè)元素找,如果有,則記錄位置,然后跳出循環(huán);否則,查找失敗。代碼如下:Private Sub Search(a(), ByVal K

45、ey, Index As Integer)Dim i%For i = LBound(a) To UBound(a)If a(i) = Key Then '找到,將元素下標(biāo)保存在index中并結(jié)束查找 Index = iExit SubEnd IfNext iIndex = -1'若沒(méi)找到,則index值為-1End Sub2) 二分法查找順序查找效率低下,當(dāng)數(shù)組有序排列時(shí),可以使用二分法查找提高效率。算法思想:代碼如下:Private Sub birSearch(a(), ByVal low%, ByVal high%, ByVal Key, index%) Dim mid A

46、s IntegerIf low > high Then'沒(méi)有查找至 Uindex = -1Exit SubEnd Ifmid = (low + high) 2 '取查找區(qū)間的中點(diǎn)If Key = a(mid) Then '查找到,返回下標(biāo)index = midExit SubElseIf Key < a(mid) Then '查找區(qū)間在上半部分high = mid - 1Elselow = mid + 1'查找區(qū)間在下半部分End IfCall birSearch(a, low, high, Key, index)'遞歸調(diào)用查找函數(shù)E

47、nd Sub調(diào)用方法:Private Sub Command1_Click()Dim a(11)a=5: a(2) = 13: a(3) = 19: a(4) = 21: a(5) = 37a(6) = 56: a(7) = 64: a(8) = 75: a(9) = 80: a(10) = 88: a(11) = 92Dim ind As IntegerCall birSearch(a, LBound(a), UBound(a), 21, ind)Print indEnd Sub2、實(shí)戰(zhàn)練習(xí)補(bǔ)充代碼(2002春二(9)(此題可以不看:蒯)C盤根目錄下stu.txt文件中以下的格式保存著學(xué)生姓

48、名及班號(hào)信息。本程序的功能是:讀取該文件中的數(shù)據(jù),再利用InputvBox輸入要查找的學(xué)生姓名,通過(guò)順序查找,給出找到 或找不到的信息。附圖是某同學(xué)被找到時(shí)顯示的信息窗口。張文,機(jī)械01李明,機(jī)械01王文卉,機(jī)械01何宇宙,電子02Option ExplicitOption Base 1Private Sub Form_click()Dim stu() As String, glass() As String, i As IntegerDim n As Integer, name As String, k As IntegerOpen "c:stustu.txt" For

49、Input As #11Do While (1)(2)ReDim Preserve stu(n), glass(n)Input #11, stu(n), glass(n)Loopname = InputBox("輸入欲查找的學(xué)生姓名:")Call search(name, stu, k)If k <= n ThenMsgBox (3)ElseMsgBox "無(wú)此人。"End IfClose #11End SubPrivate Sub search(name As String, stu() As String, k As Integer)Dim i

50、 As IntegerFor i = 1 To UBound(stu)If name = stu(i) Then(4)End IfNext i(5)End Sub(9)數(shù)學(xué)求解1、算法說(shuō)明1)初等數(shù)學(xué)遞推法又稱為迭代法”,其基本思想是把一個(gè)復(fù)雜的計(jì)算過(guò)程轉(zhuǎn)化為簡(jiǎn)單過(guò)程的多次重復(fù)。每次重復(fù)都在舊值的基礎(chǔ)上遞推出新值,并由新值代替舊值。問(wèn)題:猴子吃桃子小猴子有若干桃子,第一天吃掉一半多一個(gè);第二天吃掉剩下的一半多一個(gè).;如此,到第七天早上要吃時(shí),只剩下一個(gè)桃子。問(wèn)小猴子一開(kāi)始共有多少桃子?分析:可以最后一天桃子數(shù)推出倒數(shù)第二天的桃子數(shù);再?gòu)牡箶?shù)第二天推出倒數(shù)第三天桃子數(shù)設(shè)第n大桃子數(shù)為xn,前一天

51、桃子數(shù)是:xn -1,則有關(guān)系:xn = xn 1/2-1程序如下:Private Sub Command1_Click()Dim n%, i%x = 1 '第七天桃子數(shù)Print "第七天桃子數(shù):1只"For i = 6 To 1 Step -1x = (x + 1) * 2Print "第"& i & "天桃子數(shù):"& x & "只"Next iEnd Sub窮舉法又稱枚舉法,即將所有可能情況一一測(cè)試,判斷是否滿足條件,一般用循環(huán)實(shí)現(xiàn)。問(wèn)題:百元買雞問(wèn)題。假定小雞每只5角

52、;公雞每只2元;母雞每只3元。現(xiàn)在有100元,要求買100只雞,編程 列出所有可能的購(gòu)雞方案。分析:設(shè)母雞、公雞、小雞分別x、v、z只,則有:x + y + z= 1003x+2y+0.5z= 100程序一:Private Sub Command1_Click()Dim x%, y%, z%For x = 0 To 100For y = 0 To 100For z = 0 To 100If x + y + z = 100 And 3 * x + 2 * y + 0.5 * z = 100 Then Print x, y, z End IfNext z Next y Next x End Sub

53、 程序二(優(yōu)化)Private Sub Command1_Click() Dim x%, y% For x = 0 To 33For y = 0 To 50If 3 * x + 2 * y + 0.5 * (100 - x - y) = 100 Then Print x, y, 100 - x - yEnd If Next y Next x End Sub 2)高等數(shù)學(xué)求積分 近似計(jì)算積分:s= / 13(x3+2x+5)dx 代碼如下:Public Function f(ByVal x!)'被積函數(shù) f = x * (x * x + 2) + 5 End FunctionPublic

54、 Function trapez(ByVal a!, ByVal b!, ByVal n%) As Single 'b、a分別為積分上下限,n為等分?jǐn)?shù)Dim sum!, h!, x! h = (b - a) / n sum = (f(a) + f(b) / 2 For i = 1 To n - 1 x = a + i * h sum = sum + f(x) Next i trapez = sum * h End Function 調(diào)用:Private Sub Command1_Click()Print trapez(1,3, 30) End Sub 數(shù)論綜合題此類題目比較廣泛,必須給

55、以足夠重視。歷年題目有:平方數(shù)、零巧數(shù)、擬立滿數(shù)、回文數(shù)、酉完數(shù)、完數(shù)、幸運(yùn)數(shù)、逆序數(shù) 以及無(wú)暇素?cái)?shù)、超級(jí)素?cái)?shù)、因子和等。2、實(shí)戰(zhàn)練習(xí)1)補(bǔ)充代碼(2002秋二(10)找出由兩個(gè)不同數(shù)字組成的平方數(shù),并將結(jié)果按圖中的格式顯示在列表框Listl中Option ExplicitPrivate Sub Command1_Click()Dim I As Long, N As LongFor I = 11 To 300(1)If Verify(N) Then(2.)End IfNext IEnd SubPrivate Function Verify(3) As BooleanDim A(0 To 9)

56、As Integer, I As Integer, Js As IntegerDo While N <> 0(4)N = N 10LoopFor I = 0 To 9Js = Js + A(I)Next I(5)End Function1) 編程題( 2004秋上機(jī)試卷08)一個(gè)正整數(shù)被稱為奇妙平方數(shù),如果此數(shù)的平方與它的逆序數(shù)的平方互為逆序數(shù)。例12A2=144, 21A2=441,12與21互逆,144與441互逆,12就是奇妙平方數(shù)。找出 1-300以內(nèi)所有的奇妙平方數(shù)。奇妙的平方叵區(qū)2 2 2 2 2 2 2* * - * - .23"奠03121322144儂 =1CMQ4 =10609 s 125 氣 -127S9 =14的421*2 =】44»*£ = 981 2012 = 4 0鈍 1 3Q 2 = 90601 21K2 * 445Z1 311*2 = 96T21 然2=

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論