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

2020版算法與程序設(shè)計 專項測試九 簡單數(shù)學方法

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

2020版算法與程序設(shè)計 專項測試九 簡單數(shù)學方法

資源簡介

專項測試九 簡單數(shù)學方法
1.用蒙特卡洛法求圓周率π的近似值。下列程序是使用隨機函數(shù)Rnd,根據(jù)x2+y2≤1判斷落點是否在圓內(nèi)來求π的近似值。其中,總共投點n次,統(tǒng)計落點在單位圓的點數(shù)為s,則
4s/n的值即為所求的π值。具體功能如下:在文本框Text1中輸入n,單擊“計算”按鈕Command1,結(jié)果顯示在文本框Text2中。程序運行界面如圖所示。
蒙特卡洛法又稱統(tǒng)計模擬法,是一種使用隨機數(shù)(或偽隨機數(shù))對模擬實驗數(shù)據(jù)進行統(tǒng)計計算從而得出結(jié)論的計算方法。具體方法為:用[0,1)之間的2個隨機數(shù),根據(jù)x2+y2≤1判斷點(x,y)是否在圓內(nèi)來求π的近似值。
(1)要使程序運行時,清空文本框Text1中的內(nèi)容,可在Form_Load事件過程中添加語句________(單選,填字母:A. Text1.Text=“空” / B.Text.Text1=“ ” / C.Text1.Text=“ ” )。
(2)實現(xiàn)上述功能的VB程序如下,請在程序劃線處填入合適的代碼。
Private Sub Command1_Click()
 Dim i As Long, n As Long, s As Long
 Dim a As Double, b As Double, c As Double
 Randomize
 n = Val(Text1.Text)
 i = 1
 s = 0
 Do While i <= n
a = Rnd: b = Rnd
________①________
If c <= 1 Then
   ________②________
End If
i = i + 1
 Loop
 Text2.Text =________③________
End Sub
(3)隨著n的增大,π的精確度變化趨勢為__________(單選,填字母:A.不變 / B.變高 / C.變低 / D.無法預測)。
解析 本題主要考查的是使用蒙特卡洛法求圓周率π的近似值。根據(jù)蒙特卡洛法的算法可知,程序劃線①處的語句為c=a^2+b^2 ,也可寫作c=a*a+b*b,劃線②處的語句為統(tǒng)計落點在單位圓的點數(shù),因此此處代碼為s=s+1;4s/n的值即為所求的π值,因此③處的代碼為Str(4*s/n)。(3)隨著n的增大,π的精確度變化趨勢為變高。
答案 (1)C (2)①c=a^2+b^2或 c=a*a+b*b
②s=s+1  ③Str(4*s/n) (3)B
2.下列VB程序?qū)崿F(xiàn)如下功能:程序運行時,在文本框Text1和Text2中分別輸入x和n的值,單擊命令按鈕Command1后,調(diào)用函數(shù)f計算s的值,并在文本框Text3中顯示計算結(jié)果。
s=x+++…+。請在劃線處填入合適的代碼。
 Function f ( x As Single,n As Integer )As Single
 Dim sum As Single,i As Integer,t As Single
 sum = 0: t = 1
 For i = 1 To n
  t = t * x / i
  sum = ______①______
 Next i
 f = sum
 End Function
 Private  Sub Command1_Click ( )
 Dim x As Single,n As Integer,s As Single
 x = Val ( Text1.Text ): n = Val ( Text2.Text ): s = f ( x , n )
 Text3.Text =______②______
 End Sub
答案 ①Sum+t ②Str(s)
3.分解質(zhì)因子。一個非素數(shù)的正整數(shù)可以表示成它的質(zhì)因子(“1”與其本身除外)的乘積。輸入一個整數(shù)N,求它所有的質(zhì)因子以及每個因子出現(xiàn)的次數(shù),最后顯示乘積表達式。例如:整數(shù)150=2×3×5×5,所以它的質(zhì)因子是2,3,5,次數(shù)分別是1次,1次,2次。
具體功能如下:在文本框Text1中輸入一個數(shù),單擊“計算”命令按鈕Command1,在列表框List1中顯示該數(shù)的質(zhì)因子及相應(yīng)的次數(shù),并顯示乘積表達式。程序運行時的界面如下圖所示。
實現(xiàn)上述功能的VB程序如下,但加框處代碼有錯,請改正。
Private Sub Command1_Click()
 Dim a(1 To 100) As Integer   ′數(shù)組a存放該整數(shù)的質(zhì)因子出現(xiàn)的次數(shù)
 Dim i As Integer, m As Integer, n As Integer, k As Integer, s As String
 n = Val(Text1.Text)
 m = n
 k = Int(Sqr(n))
 For i = 2 To k
  a(i) = 0
 Next i
 For i = 2 To k
Do While n Mod i = 0
    ′①
  n = n i
  If  Then   ′②
   s = s + Str(i) + “×”
  Else
   s = s + Str(i)
   Exit For
  End If
Loop
 Next i
 List1.AddItem “質(zhì)因子” & “  ” & “次數(shù)”
 For i = 2 To k
  If a(i) < > 0 Then List1.AddItem Str(i) & “  ” & Str(a(i))
 Next i
 List1.AddItem Str(m) + “=” + s
End Sub
程序中加框①處應(yīng)改正為____________________________________________;
加框②處應(yīng)改正為________________________________________________。
解析 本題考查VB程序的綜合能力。①處語句表示計算并存儲質(zhì)因子i出現(xiàn)的次數(shù),質(zhì)因子i出現(xiàn)的次數(shù)存儲在數(shù)組元素a(i)中,因此①處語句應(yīng)改為a(i) = a(i) + 1。②處語句表示對乘積表達式的處理,當n =1時表示已求出整數(shù)n的所有質(zhì)因子,表達式最后無乘號,只需將質(zhì)因子拼接在字符串s中即可;當n< >1或n>1時,將質(zhì)因子拼接在字符串s后還需增加一個乘號。
答案 ①a(i) = a(i) + 1 ②n < > 1或n > 1
4.尼克斯徹定理: 任何一個大于等于1的整數(shù)的立方等于一串連續(xù)奇數(shù)之和,如:13 =1,
23 =3+5,33 =7+9+11。編寫一個VB程序驗證尼克斯徹定理,程序運行時,在文本框Text1中輸入一個大于等于1的整數(shù),單擊“驗證”按鈕Command1,在文本框Text2中顯示驗證結(jié)果,運行界面如下圖所示。
實現(xiàn)上述功能的 VB 代碼如下,其中加框處代碼有錯,請改正。
Private Sub Command1_Click()
 Dim n As Integer, i As Integer, t As Integer, sum As Long, s As String
 n = Val(Text1.Text)
 For i = 1 To n ^ 3 Step 2 ′ 變量 i 表示連續(xù)奇數(shù)的首個整數(shù)
sum = 0 : t =i : flag = False
Do While sum <= n^3 And flag = False
sum = sum + t
′(1)
If sum = n ^ 3 Then flag = True
Loop
If flag = True Then Exit For
 Next i
 s = Str(n) + “^” + Str(3) + “=”
 Do While sum > 0
′(2)
If sum = 0 Then
  s = s + Str(i)
Else
  s = s + Str(i) + “+”
End If
i = i + 2
 Loop
 Text2.Text = s
End Sub
程序中加框(1)處應(yīng)改正為_____________________________________________;
加框(2)處應(yīng)改正為_________________________________________________。
答案 (1)t=t+2 (2)sum=sum-i
5.猴子選大王。有n(n≤100)只猴子舉行選大王大會,由于大家誰也不服誰,因此有猴子倡議進行玩游戲的方式來選出大王。首先將n只猴子進行隨機編號(編號為1到n,猴子編號不重復),然后產(chǎn)生一個隨機數(shù)k,游戲規(guī)則如下:將n只猴子按編號從小到大的順序圍成一圈,并從編號為1的猴子開始報數(shù)(1,2,3……),報到k的猴子出圈(出圈即被淘汰);接著再由出圈的猴子的下一只猴子重新開始報數(shù)(1,2,3……),報到k的猴子又出圈,如此繼續(xù),直到圈中只剩下一只猴子,它就是猴子大王。要求依次輸出出圈的猴子的編號及猴子大王的編號。
例如n=8,k=6,則依次出圈的猴子的編號為:6、4、3、5、8、7、2,最后剩下的猴子的編號為1,即猴子大王的編號為1號。
程序運行時,在文本框Text1中在輸入猴子的總數(shù)n,單擊“產(chǎn)生隨機數(shù)k”按鈕Command1將生成一個隨機整數(shù)k(1≤k≤n),并在標簽Label3中顯示k的值,單擊“選大王”按鈕Command2后,將在文本框Text2中顯示依次出圈的猴子的編號,在標簽Label6中顯示大王的編號。程序運行結(jié)果如圖所示。
實現(xiàn)上述功能的VB程序如下,請在劃線處填入合適的代碼。
Dim k As Integer, n As Integer
Private Sub Command1_Click()
  Randomize
  n = Val(Text1.Text)
  k = Int(Rnd * n) + 1
  Label3.Caption = Str(k)
End Sub
Private Sub Command2_Click()
 Dim a(1 To 100) As Integer  ′a(i)=1表示編號為i的猴子在圈上,a(i)=0表示出圈
 Dim b(1 To 100) As Integer  ′數(shù)組元素b(i)=x,表示第i只出圈的猴子的編號為x
 Dim i As Integer, st As String, j As Integer
 Dim p As Integer, sum As Integer  ′p表示出圈的猴子數(shù),sum表示報數(shù)
 For i = 1 To n
a(i) = 1
 Next i
 p = 0
 j = 0
 Do While p < > n-1
________①________
Do While sum < k ′依次報數(shù),直到報到k為止
j = j + 1
If j > n Then j = 1 ′報數(shù)至最后時,將重頭繼續(xù)報
sum = sum + a(j)
Loop
a(j) = 0: p = p + 1
________②________
Loop
For i = 1 To p
  st = st + Str(b(i))
 Next i
Text2.Text = st
 i = 1
Do While a(i) < > 1
 i = i + 1
  Loop
  Label6.Caption =________③________
End Sub
解析 變量sum表示每次的報數(shù),一旦有猴子出圈時,將做兩個事情:一是記錄出圈的猴子的編號及出圈的序號,二是重新進行報數(shù),即變量sum需清零。因此①處應(yīng)填入的語句為sum = 0,②處應(yīng)填入的語句為b(p) = j。最后將留在圈上的猴子編號顯示在標簽Label6上,a(i)< >1表示留在圈上,因此代碼中最后的Do While循環(huán)停止時的i的值,即為最后剩下的猴子的編號,即為猴子大王,因此③處應(yīng)填入的語句為Str(i)。
答案 ①sum=0 ②b(p)=j(luò) ③Str(i)
6.在平面直角坐標系中,在給定線段AB中尋找一點X,使得X到給定的P點距離最短,求出其最短距離。
尋找點x的算法如下:
(1)取AB線段1/3處的點t1,2/3處的點t2;
(2)計算t1到P點距離ans1,t2到P點距離ans2;
(3)如果t1和t2重合,此時重合的點就是所求的點,ans1就是所求的距離,算法結(jié)束;否則,如果ans1(1)根據(jù)本題算法,若輸入的A(0,0),B(100,100),P(100,0),所求的最短距離為________。
(2)實現(xiàn)上述功能的VB程序如下,請在程序劃線處填入合適的代碼:
Dim ax As Double, ay As Double, bx As Double, by As Double, px As Double, py As Double
Dim t1x As Double, t2x As Double, t1y As Double, t2y As Double, ans1 As Double, ans2 As Double
′函數(shù)dis(x1, y1, x2, y2)的功能是求點x1和x2間的距離
Function dis(x1 As Double, y1 As Double, x2 As Double, y2 As Double)
dis = Sqr((x1 - x2) * (x1 - x2) + ____①____)
End Function
Private Sub Command1_Click()
ax = Val(Text1.Text)
ay = Val(Text2.Text)
bx = Val(Text3.Text)
by = Val(Text4.Text)
px = Val(Text5.Text)
 py = Val(Text6.Text)
 Do While True
′(t1x,t1y) 為線段1/3處點坐標
t1x = (2 * ax + bx) / 3
t1y = (2 * ay + by) / 3
′(t2x,t2y) 為線段2/3處點坐標
t2x = (ax + 2 * bx) / 3
t2y = (ay + 2 * by) / 3
ans1 = dis(t1x, t1y, px, py)
ans2 = dis(t2x, t2y, px, py)
′ t1和t2重合,循環(huán)結(jié)束
If Abs(t1x - t2x) < 0.001 And Abs(t1y - t2y)<0.001 Then Exit Do 
If ____②____ Then
bx = t2x :by = t2y
Else
ax = t1x :ay = t1y
End If
  Loop
 Label8.Caption = Str(____③____)′取整數(shù),要求四舍五入
End Sub
答案 (1)71 (2)①(y1 - y2) * (y1 - y2)
②ans1 < ans2 ③Int(ans1+ 0.5)或Int(ans2+ 0.5)
7.領(lǐng)禮物。圣誕節(jié)到了,教室里放滿了禮物,大家站在同一位置即原點(0,0)處,根據(jù)自己拿禮物的序號輪流去拿禮物,每個人只能拿離自己最近的禮物,每件禮物的位置用坐標(x,y)表示。如果有多個禮物離你的距離相等(精確到小數(shù)后二位)且最近,則這些禮物都歸你所有。
程序運行時,在文本框Text1中輸入共有禮物數(shù)n,單擊“讀取數(shù)據(jù)”按鈕Command1,將從數(shù)據(jù)庫gift.accdb中讀取n個禮物的信息存儲在相應(yīng)的數(shù)組中,并顯示在列表框List1中;在文本框Text2中輸入輪到你拿禮物的序號(即第幾個拿),單擊“計算”按鈕Command2,則在列表框List2顯示你獲得的禮物信息,并在列表框List2最后一行顯示獲得的禮物總數(shù)。程序運行界面如圖所示。
實現(xiàn)上述功能的VB程序代碼如下,請回答下列問題。
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim bh(1 To 100) As String  ′數(shù)組bh存儲禮物的編號
Dim x(1 To 100) As Single ′數(shù)組x存儲禮物的橫坐標
Dim y(1 To 100) As Single ′數(shù)組y存儲禮物的縱坐標
Dim dist(1 To 100) As Single ′數(shù)組dist存儲禮物到原點的距離
Dim n As Integer  ′變量n表示禮物數(shù)
Private Sub Command1_Click()
 Dim i As Integer, q As Integer
 conn.ConnectionString = “Provider = Microsoft.ACE.OLEDB.12.0;DATA Source=”&App.Path& “gift.mdb”
 conn.Open
 Set rs.ActiveConnection = conn
 rs.Open “l(fā)ist”
 List1.AddItem “編號” + “ x” + “  y” + “    dist”
 n = Val(Text1.Text)
 q = 0
 Do While Not rs.EOF And q q = q + 1
bh(q) = rs.Fields(“bh”)
x(q) = rs.Fields(“x”)
y(q) = rs.Fields(“y”)
dist(q) = ______①______
List1.AddItem bh(q) + “  ” + Str(x(q)) + “  ” + Str(y(q)) + “  ” + Str(dist(q))
rs.MoveNext      ′移動到下一條記錄
  Loop
  rs.Close
  conn.Close
  Set rs = Nothing
  Set conn = Nothing
End Sub
Private Sub Command2_Click()
 Dim xh As Integer, i As Integer, j As Integer, k As Integer
 Dim temp1 As String, temp2 As Single, p As Single, g As Single
 xh = Val(Text2.Text)
 ′將禮物按到原點的距離從小到大排序,代碼略
 i = 0: j = 1: g = 0
 Do While (i < xh) And (j <= n)
If dist(j) < > g Then____②____ : i = i + 1
j = j + 1
 Loop
 u = 0: d = g
 j = j - 1
 Do While ________③________
List2.AddItem bh(j) + Str(x(j)) + Str(y(j)) + Str(dist(j))
u = u + 1
j = j + 1
 Loop
 List2.AddItem “total:” + Str(u)
End Sub
(1)請在程序劃線處填入合適的語句。
(2)假設(shè)現(xiàn)共有14件禮物,禮物的信息如上圖所示,你輪到第10個拿禮物,則你獲得的禮物數(shù)為________。
解析 (1)①處代碼表示計算禮物離原點的距離,因此①處代碼為Int((Sqr(x(q) ^ 2 + y(q) ^ 2) * 100 + 0.5)) / 100;②處的循環(huán)表示你前面的同學拿走禮物的情況,前面xh-1個同學共拿走了j件禮物,劃線②處代碼的功能是比較相鄰兩個禮物是否是等距離的,因此②處代碼為g = dist(j);變量d表示你拿的禮物離你的距離,如果有多個禮物的距離等于d,則全部拿走,變量u表示你共拿走的禮物數(shù),因此③處碼為dist(j) = d。
(2)本小題是算法的實際應(yīng)用。你共拿到2件禮物,因此答案為2。
答案 (1)①Int((Sqr(x(q) ^ 2 + y(q) ^ 2) * 100 + 0.5)) / 100 ②g = dist(j) ③dist(j) = d (2)2
專項測試九 簡單數(shù)學方法
1.用蒙特卡洛法求圓周率π的近似值。下列程序是使用隨機函數(shù)Rnd,根據(jù)x2+y2≤1判斷落點是否在圓內(nèi)來求π的近似值。其中,總共投點n次,統(tǒng)計落點在單位圓的點數(shù)為s,則
4s/n的值即為所求的π值。具體功能如下:在文本框Text1中輸入n,單擊“計算”按鈕Command1,結(jié)果顯示在文本框Text2中。程序運行界面如圖所示。
蒙特卡洛法又稱統(tǒng)計模擬法,是一種使用隨機數(shù)(或偽隨機數(shù))對模擬實驗數(shù)據(jù)進行統(tǒng)計計算從而得出結(jié)論的計算方法。具體方法為:用[0,1)之間的2個隨機數(shù),根據(jù)x2+y2≤1判斷點(x,y)是否在圓內(nèi)來求π的近似值。
(1)要使程序運行時,清空文本框Text1中的內(nèi)容,可在Form_Load事件過程中添加語句________(單選,填字母:A. Text1.Text=“空” / B.Text.Text1=“ ” / C.Text1.Text=“ ” )。
(2)實現(xiàn)上述功能的VB程序如下,請在程序劃線處填入合適的代碼。
Private Sub Command1_Click()
 Dim i As Long, n As Long, s As Long
 Dim a As Double, b As Double, c As Double
 Randomize
 n = Val(Text1.Text)
 i = 1
 s = 0
 Do While i <= n
a = Rnd: b = Rnd
________①________
If c <= 1 Then
   ________②________
End If
i = i + 1
 Loop
 Text2.Text =________③________
End Sub
(3)隨著n的增大,π的精確度變化趨勢為__________(單選,填字母:A.不變 / B.變高 / C.變低 / D.無法預測)。
2.下列VB程序?qū)崿F(xiàn)如下功能:程序運行時,在文本框Text1和Text2中分別輸入x和n的值,單擊命令按鈕Command1后,調(diào)用函數(shù)f計算s的值,并在文本框Text3中顯示計算結(jié)果。
s=x+++…+。請在劃線處填入合適的代碼。
 Function f ( x As Single,n As Integer )As Single
 Dim sum As Single,i As Integer,t As Single
 sum = 0: t = 1
 For i = 1 To n
  t = t * x / i
  sum = ______①______
 Next i
 f = sum
 End Function
 Private  Sub Command1_Click ( )
 Dim x As Single,n As Integer,s As Single
 x = Val ( Text1.Text ): n = Val ( Text2.Text ): s = f ( x , n )
 Text3.Text =______②______
 End Sub
3.分解質(zhì)因子。一個非素數(shù)的正整數(shù)可以表示成它的質(zhì)因子(“1”與其本身除外)的乘積。輸入一個整數(shù)N,求它所有的質(zhì)因子以及每個因子出現(xiàn)的次數(shù),最后顯示乘積表達式。例如:整數(shù)150=2×3×5×5,所以它的質(zhì)因子是2,3,5,次數(shù)分別是1次,1次,2次。
具體功能如下:在文本框Text1中輸入一個數(shù),單擊“計算”命令按鈕Command1,在列表框List1中顯示該數(shù)的質(zhì)因子及相應(yīng)的次數(shù),并顯示乘積表達式。程序運行時的界面如下圖所示。
實現(xiàn)上述功能的VB程序如下,但加框處代碼有錯,請改正。
Private Sub Command1_Click()
 Dim a(1 To 100) As Integer   ′數(shù)組a存放該整數(shù)的質(zhì)因子出現(xiàn)的次數(shù)
 Dim i As Integer, m As Integer, n As Integer, k As Integer, s As String
 n = Val(Text1.Text)
 m = n
 k = Int(Sqr(n))
 For i = 2 To k
  a(i) = 0
 Next i
 For i = 2 To k
Do While n Mod i = 0
    ′①
  n = n i
  If  Then   ′②
   s = s + Str(i) + “×”
  Else
   s = s + Str(i)
   Exit For
  End If
Loop
 Next i
 List1.AddItem “質(zhì)因子” & “  ” & “次數(shù)”
 For i = 2 To k
  If a(i) < > 0 Then List1.AddItem Str(i) & “  ” & Str(a(i))
 Next i
 List1.AddItem Str(m) + “=” + s
End Sub
程序中加框①處應(yīng)改正為____________________________________________;
加框②處應(yīng)改正為________________________________________________。
4.尼克斯徹定理: 任何一個大于等于1的整數(shù)的立方等于一串連續(xù)奇數(shù)之和,如:13 =1,
23 =3+5,33 =7+9+11。編寫一個VB程序驗證尼克斯徹定理,程序運行時,在文本框Text1中輸入一個大于等于1的整數(shù),單擊“驗證”按鈕Command1,在文本框Text2中顯示驗證結(jié)果,運行界面如下圖所示。
實現(xiàn)上述功能的 VB 代碼如下,其中加框處代碼有錯,請改正。
Private Sub Command1_Click()
 Dim n As Integer, i As Integer, t As Integer, sum As Long, s As String
 n = Val(Text1.Text)
 For i = 1 To n ^ 3 Step 2 ′ 變量 i 表示連續(xù)奇數(shù)的首個整數(shù)
sum = 0 : t =i : flag = False
Do While sum <= n^3 And flag = False
sum = sum + t
′(1)
If sum = n ^ 3 Then flag = True
Loop
If flag = True Then Exit For
 Next i
 s = Str(n) + “^” + Str(3) + “=”
 Do While sum > 0
′(2)
If sum = 0 Then
  s = s + Str(i)
Else
  s = s + Str(i) + “+”
End If
i = i + 2
 Loop
 Text2.Text = s
End Sub
程序中加框(1)處應(yīng)改正為_____________________________________________;
加框(2)處應(yīng)改正為_________________________________________________。
5.猴子選大王。有n(n≤100)只猴子舉行選大王大會,由于大家誰也不服誰,因此有猴子倡議進行玩游戲的方式來選出大王。首先將n只猴子進行隨機編號(編號為1到n,猴子編號不重復),然后產(chǎn)生一個隨機數(shù)k,游戲規(guī)則如下:將n只猴子按編號從小到大的順序圍成一圈,并從編號為1的猴子開始報數(shù)(1,2,3……),報到k的猴子出圈(出圈即被淘汰);接著再由出圈的猴子的下一只猴子重新開始報數(shù)(1,2,3……),報到k的猴子又出圈,如此繼續(xù),直到圈中只剩下一只猴子,它就是猴子大王。要求依次輸出出圈的猴子的編號及猴子大王的編號。
例如n=8,k=6,則依次出圈的猴子的編號為:6、4、3、5、8、7、2,最后剩下的猴子的編號為1,即猴子大王的編號為1號。
程序運行時,在文本框Text1中在輸入猴子的總數(shù)n,單擊“產(chǎn)生隨機數(shù)k”按鈕Command1將生成一個隨機整數(shù)k(1≤k≤n),并在標簽Label3中顯示k的值,單擊“選大王”按鈕Command2后,將在文本框Text2中顯示依次出圈的猴子的編號,在標簽Label6中顯示大王的編號。程序運行結(jié)果如圖所示。
實現(xiàn)上述功能的VB程序如下,請在劃線處填入合適的代碼。
Dim k As Integer, n As Integer
Private Sub Command1_Click()
  Randomize
  n = Val(Text1.Text)
  k = Int(Rnd * n) + 1
  Label3.Caption = Str(k)
End Sub
Private Sub Command2_Click()
 Dim a(1 To 100) As Integer  ′a(i)=1表示編號為i的猴子在圈上,a(i)=0表示出圈
 Dim b(1 To 100) As Integer  ′數(shù)組元素b(i)=x,表示第i只出圈的猴子的編號為x
 Dim i As Integer, st As String, j As Integer
 Dim p As Integer, sum As Integer  ′p表示出圈的猴子數(shù),sum表示報數(shù)
 For i = 1 To n
a(i) = 1
 Next i
 p = 0
 j = 0
 Do While p < > n-1
________①________
Do While sum < k ′依次報數(shù),直到報到k為止
j = j + 1
If j > n Then j = 1 ′報數(shù)至最后時,將重頭繼續(xù)報
sum = sum + a(j)
Loop
a(j) = 0: p = p + 1
________②________
Loop
For i = 1 To p
  st = st + Str(b(i))
 Next i
Text2.Text = st
 i = 1
Do While a(i) < > 1
 i = i + 1
  Loop
  Label6.Caption =________③________
End Sub
6.在平面直角坐標系中,在給定線段AB中尋找一點X,使得X到給定的P點距離最短,求出其最短距離。
尋找點x的算法如下:
(1)取AB線段1/3處的點t1,2/3處的點t2;
(2)計算t1到P點距離ans1,t2到P點距離ans2;
(3)如果t1和t2重合,此時重合的點就是所求的點,ans1就是所求的距離,算法結(jié)束;否則,如果ans1(1)根據(jù)本題算法,若輸入的A(0,0),B(100,100),P(100,0),所求的最短距離為________。
(2)實現(xiàn)上述功能的VB程序如下,請在程序劃線處填入合適的代碼:
Dim ax As Double, ay As Double, bx As Double, by As Double, px As Double, py As Double
Dim t1x As Double, t2x As Double, t1y As Double, t2y As Double, ans1 As Double, ans2 As Double
′函數(shù)dis(x1, y1, x2, y2)的功能是求點x1和x2間的距離
Function dis(x1 As Double, y1 As Double, x2 As Double, y2 As Double)
dis = Sqr((x1 - x2) * (x1 - x2) + ____①____)
End Function
Private Sub Command1_Click()
ax = Val(Text1.Text)
ay = Val(Text2.Text)
bx = Val(Text3.Text)
by = Val(Text4.Text)
px = Val(Text5.Text)
 py = Val(Text6.Text)
 Do While True
′(t1x,t1y) 為線段1/3處點坐標
t1x = (2 * ax + bx) / 3
t1y = (2 * ay + by) / 3
′(t2x,t2y) 為線段2/3處點坐標
t2x = (ax + 2 * bx) / 3
t2y = (ay + 2 * by) / 3
ans1 = dis(t1x, t1y, px, py)
ans2 = dis(t2x, t2y, px, py)
′ t1和t2重合,循環(huán)結(jié)束
If Abs(t1x - t2x) < 0.001 And Abs(t1y - t2y)<0.001 Then Exit Do 
If ____②____ Then
bx = t2x :by = t2y
Else
ax = t1x :ay = t1y
End If
  Loop
 Label8.Caption = Str(____③____)′取整數(shù),要求四舍五入
End Sub
7.領(lǐng)禮物。圣誕節(jié)到了,教室里放滿了禮物,大家站在同一位置即原點(0,0)處,根據(jù)自己拿禮物的序號輪流去拿禮物,每個人只能拿離自己最近的禮物,每件禮物的位置用坐標(x,y)表示。如果有多個禮物離你的距離相等(精確到小數(shù)后二位)且最近,則這些禮物都歸你所有。
程序運行時,在文本框Text1中輸入共有禮物數(shù)n,單擊“讀取數(shù)據(jù)”按鈕Command1,將從數(shù)據(jù)庫gift.accdb中讀取n個禮物的信息存儲在相應(yīng)的數(shù)組中,并顯示在列表框List1中;在文本框Text2中輸入輪到你拿禮物的序號(即第幾個拿),單擊“計算”按鈕Command2,則在列表框List2顯示你獲得的禮物信息,并在列表框List2最后一行顯示獲得的禮物總數(shù)。程序運行界面如圖所示。
實現(xiàn)上述功能的VB程序代碼如下,請回答下列問題。
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim bh(1 To 100) As String  ′數(shù)組bh存儲禮物的編號
Dim x(1 To 100) As Single ′數(shù)組x存儲禮物的橫坐標
Dim y(1 To 100) As Single ′數(shù)組y存儲禮物的縱坐標
Dim dist(1 To 100) As Single ′數(shù)組dist存儲禮物到原點的距離
Dim n As Integer  ′變量n表示禮物數(shù)
Private Sub Command1_Click()
 Dim i As Integer, q As Integer
 conn.ConnectionString = “Provider = Microsoft.ACE.OLEDB.12.0;DATA Source=”&App.Path& “gift.mdb”
 conn.Open
 Set rs.ActiveConnection = conn
 rs.Open “l(fā)ist”
 List1.AddItem “編號” + “ x” + “  y” + “    dist”
 n = Val(Text1.Text)
 q = 0
 Do While Not rs.EOF And q q = q + 1
bh(q) = rs.Fields(“bh”)
x(q) = rs.Fields(“x”)
y(q) = rs.Fields(“y”)
dist(q) = ______①______
List1.AddItem bh(q) + “  ” + Str(x(q)) + “  ” + Str(y(q)) + “  ” + Str(dist(q))
rs.MoveNext      ′移動到下一條記錄
  Loop
  rs.Close
  conn.Close
  Set rs = Nothing
  Set conn = Nothing
End Sub
Private Sub Command2_Click()
 Dim xh As Integer, i As Integer, j As Integer, k As Integer
 Dim temp1 As String, temp2 As Single, p As Single, g As Single
 xh = Val(Text2.Text)
 ′將禮物按到原點的距離從小到大排序,代碼略
 i = 0: j = 1: g = 0
 Do While (i < xh) And (j <= n)
If dist(j) < > g Then____②____ : i = i + 1
j = j + 1
 Loop
 u = 0: d = g
 j = j - 1
 Do While ________③________
List2.AddItem bh(j) + Str(x(j)) + Str(y(j)) + Str(dist(j))
u = u + 1
j = j + 1
 Loop
 List2.AddItem “total:” + Str(u)
End Sub
(1)請在程序劃線處填入合適的語句。
(2)假設(shè)現(xiàn)共有14件禮物,禮物的信息如上圖所示,你輪到第10個拿禮物,則你獲得的禮物數(shù)為________。

展開更多......

收起↑

資源列表

<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. 主站蜘蛛池模板: 庆云县| 沾益县| 喀什市| 宜宾县| 肇源县| 吴川市| 波密县| 祥云县| 威信县| 中江县| 东台市| 余江县| 喀喇沁旗| 游戏| 思茅市| 武川县| 邵武市| 桐梓县| 马公市| 东乡县| 金川县| 麻栗坡县| 隆化县| 阿巴嘎旗| 呼玛县| 西乌珠穆沁旗| 招远市| 缙云县| 松江区| 定兴县| 清涧县| 广水市| 金坛市| 邢台县| 宜川县| 铜山县| 封丘县| 巴中市| 安岳县| 大关县| 伽师县|