16.編寫VB程序,實(shí)現(xiàn)如下功能:在文本框Text1中輸入一個(gè)整數(shù),單擊“查找”按鈕,找出該整數(shù)的全部的連續(xù)整數(shù)固定和,并將它們顯示在列表框List1中.所謂一個(gè)數(shù)n的連續(xù)整數(shù)固定和,就是指存在a1,a2,…,an,其中ai+1比ai大1,使得a1+a2+…+an=n.這樣a1,a2,…,an稱為n的一個(gè)連續(xù)整數(shù)固定和.例如27的全部的連續(xù)整數(shù)固定和有3組,運(yùn)行界面如圖所示,實(shí)現(xiàn)上述功能的VB代碼如下,但加框處代碼有錯(cuò),請(qǐng)改正. Private Sub Command1_Click ( ) Dim i As Integer,j As Integer,sum As Integer Dim n As Integer n=Val(Text1.Text) sum=0 List1.Clear For i=1To n'① j=i-1 Do While sum<=n'② j=j+1 sum=sum+j Loop If sum=n Then List1.AddItem Str(i) &“+…+“& Str(j) &“=“& Str(n) End If sum=0 Next i End Sub.
實(shí)現(xiàn)上述功能的VB代碼如下. (1)請(qǐng)?jiān)跈M線處填入合適代碼 Dim a(1To 8)As String Private Sub Command1_Click ( ?。?br />Dim temp As Integer,i As Integer,j As Integer Dim flag(1To 8)As Boolean For i=1To 8
Next i Randomize For i=1To 8 temp=Int(Rnd ( ?。?8+1)'生成1~8之間的隨機(jī)整數(shù) Do While flag(temp) temp=Int(Rnd ( ?。?8+1)'生成1~8之間的隨機(jī)整數(shù) Loop a(i)=temp flag(temp)=True Next i List1.Clear List1.AddItem“初始狀態(tài):“ PrintCircle End Sub Private Sub Command2_Click ( ?。?br />Dim i As Integer,j As Integer,temp As String,n As Integer,moveStep As String moveStep=““ List2.Clear n=0 For i=1To 7'使用冒泡排序?qū)A圈中的數(shù)字進(jìn)行移動(dòng) For j=1To
If a(j)>=a(j+1)Then temp=a(j):a(j)=a(j+1):a(j+1)=temp moveStep=Str(j) &“#-->0#“ moveStep=moveStep &““& Str(j+1)&“#-->“& Str(j) &“#“moveStep=moveStep &““&“0#-->“& Str(j+1)&“#“ List2.AddItem moveStep n=n+1 End If Next j Next i List1.AddItem“移動(dòng)數(shù)字后:“ PrintCircle Label1.Caption=“總移動(dòng)步數(shù):“&
End Sub '在列表框List1中輸出當(dāng)前的數(shù)陣狀態(tài) Function PrintCircle ( ?。?br />List1.AddItem“-------------------------“ List1.AddItem“[“& a(1)&“]-[“& a(2)&“]-[“& a(3)&“]“ List1.AddItem“|\|/|“ List1.AddItem“[“& a(8)&“]-[“&““&“]-[“& a(4)&“]“ List1.AddItem“|/|\|“ List1.AddItem“[“& a(7)&“]-[“& a(6)&“]-[“& a(5)&“]“ List1.AddItem“-------------------------“ End Function (2)分析代碼和游戲規(guī)則,事件處理過(guò)程“Command2_Click”中的冒泡排序