1、 3. 统计字符串中“O”的个数 a = THERE IS A BOOK ON THE DESK m = len(a) For i = 1 To m b =mid(a,i,1) If b = O Then num=num+1 Print num4.从给定的字符串中只分离出数字 Dim s As String, t As String Dim i As Integer s = Text1.Text If Mid(s, i, 1) = 0 And Mid(s, i, 1) 9 Then g = Chr(55 + Val(g) t = g & n = n 165、(三)将正整数转化成8进制 t =
2、n Mod 8 & n = n 86.找出100以内的所有勾股数(不包括100) Dim a As Integer, b As Integer, c As Integer, n As Integer For a = 1 To 99 For b = 1 To 99 For c = 1 To 99 If a 2 + b 2 = c 2 And a b Then n = n + 1 Print a; b; c; “; If n Mod 5 = 0 Then Next c Next b Next a Print n7.找出三位数字中的所有升序数(各位数字十位数字百位数字) For a = 1 To 9
3、 For b = 1 To 9 For c = 1 To 9 If a b And b c Then Print a * 100 + b * 10 + c; If n Mod 10 = 0 Then 控制每行打印10个或Dim n As Integer, a1 As Integer, a2 As Integer, a3 As Integer, js As Integer For n = 100 To 999 a1 = n 100 取出百位数字 a2 = n 10 Mod 10 取出十位数字 a3 = n Mod 10 取出个位数字 If a1 a2 And a2 =100,b=30000Pri
4、vate sub command1_click() Dim a as integer,b as integer,i as integer a=text1:b=text2 For i=a to b If sx(i) then List1.additem i End ifEnd sub Private sub function sx(byval n as integer) as boolean Dim a() as integer,k as integer,i as integer K=k+1 Redim preserve a(k) a(k)=n mod 10 n=n10 loop until n
5、=a(i) then exit function (应该去判断a(1)a(2)a(k)但是不好判断,遇难则反,只要有一个a(i+1)=a(i)这不是升序数) next i sx=trueend function8.最小公倍数Dim m%, n%, tem%m = Val(Text1.Text)n = Val(Text2.Text)tem = mDo While tem Mod n 0 tem = tem + mLoopPrint m;和 n;的最小公倍数是: tem9.求一维数组a(n)各元素的和:Dim s as integer , I as intgers=0 for i=1 to n s
6、=s+a(i)10.求二维数组a(n,m)各元素的和Dim s as integer, i as integer, j as integerFor i=1 to n for j=1 to m s=s+a(i,j) next jNext i11. 求二维数组a(m,n)周边元素的和Dim s as integer, i as integerS=0 s=s+a(1,i)+a(m,i) (求第一行与最后一行的和) for i=2 to m-1 s=s+a(i,1)+a(i,n) (求第二行到倒数第二行第一列与最后一列的和)12、求二维数组a(n,n)主对角线元素的和(理解主对角线)Dim s as
7、integer,i as integer s=s+ a (i,i)13、求二维数组a(n,n)次对角线元素的 Dim a as integer, i as integer s=s+a(i,n+1-i)14、三种排序法(1)选择排序法是逐个比较,逆序交换;冒泡法是两两比较,逆序交换.(2)直接排序法(以降序为例)特点:比较后不立即互换元素,而是记下较大值的位置,并在每一轮比较完毕后和()互换选择排序法(比较排序法)基本过程(以降序为例):将第一个元素顺序与其后面的元素比较,比第一个大则进行交换,第一轮完毕后,最大的元素被挪到了第一个位置,第二轮从第二个元素开始重复上面的过程,结束后得到第二个最大
8、的元素,如此下去经过 N-1 轮的比较,可将 N 个数排好(3)冒泡法排序(以升序为例),则方法为:将相邻两个数比较,把小数对调到前边,如此进行一轮后,就会把最大的数互换到最后,再进行一次,则会把第二大数排在倒数第二的位置上,进行次后,整个数列即可排好在这种排序过程中,小数如同气泡一样逐层上浮,而大数逐个下沉,因此,被形象的喻为“冒泡”选择法对数组经行降序排列Option explicitOption base 1 Dim sort(10) as integer, temp as integer , i as integer, j as integer For i=1 to 10 Sort(i
9、)=int(rnd*99)+1 Text1=text1 & str(sort(i) For i= 1 to 9 For j=i+1 to 10 If sort(i)sort(j) then Temp=a(i)a(i)=a(j)a(j)=temp end if next j text2=text2 &end sub直接排序法的精髓(降序)For i=1 to N-1 pointer=I 初始化pointer,在每轮比较开始处 for j=I+1 to N if Sort(pointer) Sort(j) then pointer= j next j if I S(j+1) then t=S(j)
10、 S(j)=S(j+1) S(j+1)=t 立即互换 end ifnext i15、求三个数的和!Private Sub ifnum(str1 As String, n As Integer) Dim k As Integer For k = 1 To Len(str1) 逐个字符判断是否为数字 If Asc(Mid(str1, k, 1) 57 Then n = 0: Exit Sub Next k n = 1Dim a As String, b As String, c As StringDim y1 As Integer, y2 As Integer, y3 As Integera =
11、Text1.Text a,b,c用来取得三个文本框的字符串b = Text2.Textc = Text3.TextCall ifnum(a, y1) 用来判断a,b,c是否为数字串,为数字串时y1,y2,y3都为1Call ifnum(b, y2)Call ifnum(c, y3)If y1 = 1 And y2 = 1 And y3 = 1 Then Text4.Text = Val(a) + Val(b) + Val(c)Else Text4.Text = 三个文本框内有非数字串End If16.求三个数的最小公倍数 Dim A As Integer, B As Integer Dim L
12、 As Long, C As Integer A = Text1 B = Text2 C = Text3 L = LCM(LCM(A, B), C) Text4 = LPrivate Function LCM(ByVal X As Integer, ByVal Y As Integer) Dim M As Long, Flg As Boolean Flg = False Do Until Flg M = M + X If M Mod Y = 0 Then Flg = True Loop LCM = MEnd Function17.打印九九乘法表Private sub form_click Di
13、m I as integer, j as integer For i=1 to 9 For j=1 to i Print j;”*”I;”=”i*j; Next j print18判断完数(一个数如果恰好等于他所有因子之和,这个数就是完数。一个数的因子是指除了该数本身以外能够整除该数的数) Dim m as integer, i as integer, j as integer,sum as integer Dim a() M=text1 For i= 1 to m-1 If m mod i=0 then Sum=sum+i J=j+1 Redim preserve a(j) a(j)=i i
14、f m=sum then picture1.print m & “是完数,因为” picture1.print m;”=” for i=1 to ubound(a)-1 picture1.print a(i); “+”; next i picture1.print a(i) else “不是完数” end if end sub19.判断亲密对数(甲数的所有因子之和等于乙数,乙数的所有因子之和等于甲数)-找出5000以内的亲密对数 Dim i as integer,sum1 as integer,sum2 as integer For i=1 to 5000 Call sum_factors(I
15、,sum1) Call sum factors(sum1,sum2) If i=sum2 and isum1 then Text1=text1 & I & “,” & sum1 & vbcrlf Private sub sum_factors(byval n as integer,sum as integer) Dim I as integer Sum=0 For i=1 to n-1 If n mod i=0 then20.判断孪生素数(a,b都是素数,且|a-b|=2)例求x到y之间的孪生素数 Dim x as integer,I as integer,y as integer x=tex
16、t1:y=text2 for i=x to y-2 if judge(i) and judge(i+2) then list1.additem I & “ “ & i+2private function judge(n as integer) as Boolean dim i as integer for i=2 to n-1 if n mod i=0 then exit functionjudge=true21.求armstrong数(一个n位的正整数其各位的n次方只和等于这个数)如153=13+53+33 Dim b as integer,I as integer,c as integer
17、,t as integer,s as double Dim j() as integer For i=10 to 9999 C=len(cstr(i) S=0 For b=1 to c Redim preserve j(b) j(b)=val(mid(i,b,1) s=s+j(b)c next b if i=s then text1=text1 & i & “=” for t=1 to c-1 text1=text1 & j(b) & “” & c & “+” next t j(c) &*补充说明分解数字的两种方法首先提取的是个位数字Do K=k+1 Redim preserve a(k) a
18、(k)=n mod 10 n=n10loop until n 首先提取的是最高位数S=cstr(n) For i=1 to len(s)Redim preserve a(len(s) a(i)=mid(s,i,1)22.判断自守数(是指这个数平方运算后尾数等于该数自身的自然数) Dim x as long x=val(text1) if zi(x) then list1.additem x msgbox str(x) & “不是自守数。”private function zi(x as long) as boolean dim y as integer,s as integer,z as st
19、ring zi=false y=x*x s=len(sctr(x) if x=val(right(cstr(y),s) then zi=true23.求一个n为正整数的反序数(1234-4321)如果一个数的末尾为0,这该数没反序数 Dim n as long n=text1 if right(cstr(n),1)=0 then text2=”尾数为0,没有反序数” text2=nx(n)private function nx(n as long) as long dim st as string,s as integer,i as integer s=len(cstr(n) (统计正整数n的长度) for i=s to 1 step-1 st=st & mid(cstr(n),i,1)nx=val(st) dim k as integer,st as string do k=n mod 10 k nx=val(st)24.利用通用过程求最值private sub command1_click()dim x(10) as integer,maxv as integer,minv as integer ,i as integerfor i= 1 to 10 x(i)=int(90*rnd)+10 str(x(i)nex
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1