《常见算法代码总结》由会员分享,可在线阅读,更多相关《常见算法代码总结(4页珍藏版)》请在金锄头文库上搜索。
1、常见算法代码总结: 1、 交换 Tmp=a; A=b; B=tmp; 2、 整数 X=int(X) 3、 奇数、偶数 X mod 2=0 为偶数 X mod 2 a(j) Then 形成递增序列temp = a(i)a(i) = a(j)/p;/a(j) = tempEnd IfNext jNext i End Sub 7、 冒泡排序(书本 P157 例 7-8) Option Explicit Option Base 1 Private Sub Command1_Click()Dim A() As Integer, temp As IntegerDim i As Integer, j As
2、IntegerDim N As IntegerN = 6ReDim A(N)RandomizeFor i = 1 To N 生成数组A(i) = Int(Rnd * 99) + 1Text1.Text = Text1.Text Sum End Sub 或 k = val(Text1.text) Do While k 0sum = sum + k Mod 10k = k 10 Loop Print sum 例:找水仙花数 P77 例 5-9 11、进制转换 例:十进制数转换为二进制数 Option Base 0 Private Sub Command1_Click()Dim i As Integ
3、er, d As IntegerDim b() As Integer, s As String, k As Integerd = Text1.TextIf d = 0 ThenText2.Text = 0ElseIf d 0 ThenDo Until d = 0i = i + 1ReDim Preserve b(i)b(i) = d Mod 2d = d 2LoopFor k = i To 1 Step -1s = s & b(k)Next kEnd If End Sub 12、质因子 Do Until x = 1If x Mod k= 0 ThenList1.AddItem kx = x kElsek = k +1End IfLoop 13、求级数例 2:利用级数展开来求函数 ex的值,丢弃所有小于 0.00001 的数据项。ex=1+x+x2/2!+xn/n!+Privete Sub Command1_Click()Dim y as integer,x as integer, t as singleDim s as single,n as integerX=Text1.textY=1N=0S=1DoN=n+1T=xn 或 t=t*xS=s*nIf t/s10-5 then exit doY=y+t/sLoopText2.text=y End sub