16.【加試題】尼克斯徹定理:任何一個大于等于1的整數(shù)的立方等于一串連續(xù)奇數(shù)之和,如:13=1,23=3+5,33=7+9+11.編寫一個VB程序驗證尼克斯徹定理,程序運行時,在文本框Text1中輸入 一個大于等于1的整數(shù),單擊“驗證”按鈕Commandl,在文本框Text2中顯示驗證結(jié)果,運行界面如圖所示。 實現(xiàn)上述功能的VB代碼如下,其中加框處代碼有錯,請改正。 Dim n As Integer,i As Integer,t As Integer,sum As Long,s As string n=Val(Text1.Text) For i=1 Ton^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。
. (2)請在橫線處填上合適的代碼. Const max=1000 Const s=“物化生政史地技“ Dim n As Integer'存放學(xué)生人數(shù) Dim xjfh(1 To max)As String,xm(1 To max)As String '數(shù)組xjfh存放學(xué)生學(xué)籍輔號,數(shù)組xm存放學(xué)生姓名 Dim bj(1 To max)As Integer,xk(1 To max)As String '數(shù)組bj存放學(xué)生班級,數(shù)組xk存儲學(xué)生的選課標志 Dim zh(1 To 35)As String,tj(1 To 35)As Integer '數(shù)組zh存儲不同選課組合,數(shù)組tj存儲某一組合選課的學(xué)生數(shù) Function adj(a As String,n As Integer)As String '函數(shù)功能:在被顯示數(shù)據(jù)之后添加上合適的空格,代碼略 End Function Function convert(x As String)As String Dim i As Integer,st As String st=““ For i=1 To Len(x) If Mid(x,i,1)=“1“Then st=st+Mid(s,i,1) Next i convert=st End Function Private Sub Form_Load( ?。?br /> Dim i As Integer,km As String Dim conn As New ADODB,Connection Dim rs As New ADODB,Recordset conn.ConnectionString=“provider=microsoft.ace.jye.ai.12.0;data source=“+App.Path+“\student.jye.ai“ conn.Open Set rs.ActiveConnection=conn rs.Open“select*from stu_info“ n=0 Do While Not rs.EOF n=n+1 xjfh(n)=rs.Fields(0):xm(n)=rs.Fields(1) bj(n)=rs.Fields(2):xk(n)=rs.Fields(3)
List1.AddItem xjfh(n)+adj(xm(n),4)+adj(Str(bj(n)),4)+adj(km,6) rs.MoveNext Loop '剩余部分代碼略 功能:產(chǎn)生35種選課組合的“0”、“1”數(shù)串,存數(shù)組zh;數(shù)組tj各元素初始化為0 End Sub Private Sub Command1_Click( ?。?br /> Dim i As Integer,j As Integer,temp2 As Integer,temp1 As Integer For i=1 To 35 For j=1 To n If xk(j)=zh(i) Then
Next j Next i For i=1 To 34 For j=35 To i+1 Step-1 If
Then temp1=zh(j):zh(j)=zh(j-1):zh(j-1)=temp1 temp2=tj(j):tj(j)=tj(j-1):tj(j-1)=temp2 End If Next j Next i For i=1 To 35 List2.Str(i)+adj(convert(zh(i)),6)+adj(Str(tj(1)),6) Next i End Sub.