




已阅读5页,还剩33页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
VB编程练习1、 找出介于a和b之间所有能构成幻影素数的数。Option ExplicitPrivate Sub Command1_Click()Dim i As Integer, js As Integer Dim t As Integer, a As Integer, b As Integer a = Text1: b = Text2 List1.AddItem 幻影素数对: For i = a To b If Prime(i) Then t = Change(i) If Prime(t) And i t Then List1.AddItem i & 与 & t & 是幻影素数对 js = js + 1 End If End If Next i If js = 0 Then List1.List(0) = 本区间无幻影素数End SubPrivate Function Prime(k As Integer) As BooleanDim i As Integer For i = 2 To Sqr(k) If k Mod i = 0 Then Exit Function Next i Prime = TrueEnd FunctionPrivate Function Change(ByVal t As Integer) As IntegerDim s As String Do Until t = 0 s = s & CStr(t Mod 10) t = t 10 Loop Change = Val(s)End Function2、 验证任意一个不超过9位的自然数,经过下述的反复变换最终得到123。123称为陷阱数。Option ExplicitPrivate Sub Command1_Click() Dim n As Long n = Text1 List1.AddItem n Do While n 123 Call validate(n) List1.AddItem n Loop List1.AddItem 验证成功!End SubPrivate Sub validate(n As Long) Dim a As Integer, b As Integer, c As Integer Dim i As Integer, t As Integer c = Len(CStr(n) For i = 1 To c If Mid(CStr(n), i, 1) Mod 2 = 0 Then a = a + 1 Else b = b + 1 End If Next i If a = 0 Then t = a: a = b: b = t End If n = a * 100 + b * 10 + cEnd Sub3、 已知某个N阶方阵的元素均为150之间的整数。编写程序,找出该方阵(二维数组)的所有凸点。Option ExplicitOption Base 1Private Sub command1_click() Dim a() As Integer, i As Integer, j As Integer, n As Integer n = InputBox(输入方阵的阶 n :, , 5) ReDim a(n, n) Randomize For i = 1 To n For j = 1 To n a(i, j) = Int(50 * Rnd) + 1 Text1 = Text1 & Right( & a(i, j), 3) Next j Text1 = Text1 & vbCrLf Next i Call Look_for(a)End SubPrivate Sub Look_for(a() As Integer) Dim ub As Integer, M As Integer, L As Integer Dim i As Integer, j As Integer, k As Integer, js As Integer ub = UBound(a, 1) Text2 = 方阵的凸点有: For i = 1 To ub M = a(i, 1): L = 1 For j = 2 To ub If a(i, j) M Then M = a(i, j) L = j End If Next j For k = 1 To ub If a(k, L) M Then Exit For Next k If k ub Then js = js + 1 Text2 = Text2 & vbCrLf & a( & i & , & L & ) End If Next i If js = 0 Then Text2 = 方阵没有凸点End Sub4、随机生成由两位整数组成的4行5列的二维数组,求它的标记数组。Option ExplicitDim a(4, 5) As IntegerPrivate Sub Command1_Click() Dim i As Integer, j As Integer For i = 1 To 4 For j = 1 To 5 a(i, j) = Int(Rnd * 90) + 10 Picture1.Print a(i, j); Next j Picture1.Print Next iEnd SubPrivate Sub command2_click() Dim sign(4, 5) As String * 1, i As Integer, j As Integer Dim av As Single av = avr(a) For i = 1 To 4 For j = 1 To 5 If a(i, j) av Then sign(i, j) = G ElseIf a(i, j) av Then sign(i, j) = L Else sign(i, j) = E End If Picture2.Print sign(i, j); ; Next j Picture2.Print Next iEnd SubPrivate Function avr(a() As Integer) As Single Dim sum As Integer, i As Integer, j As Integer For i = 1 To 4 For j = 1 To 5 sum = sum + a(i, j) Next j Next i avr = sum / (4 * 5)End Function5、查找介于m与n之间的所有凸点数。所谓凸点数是一个不含数字0的5位整数。Option ExplicitPrivate Sub Command1_Click() Dim m As Long, n As Long, i As Long, j As Long Dim js As Integer m = Text1: n = Text2 For i = m To n If validate(i) Then List1.AddItem i js = js + 1 End If Next i If js = 0 Then List1.AddItem 无凸点数End SubPrivate Function validate(n As Long) As Boolean Dim s As String, a(5) As Integer, i As Integer Dim k As Boolean s = n For i = 1 To Len(s) a(i) = Val(Mid(s, i, 1) If a(i) = 0 Then Exit Function Next i k = a(1) a(2) And a(2) a(4) And a(4) a(5) If k Then validate = TrueEnd Function6、随机生成一个n行(n由InputBox函数输入,缺省值为5)2列的两位整数数组。将第1列元素作为排序主关键字,第2列元素作为排序次关键字,以行为单位对数组按从小到大进行排序,先按主关键字排序,主关键字相同则按次关键字排序。Option ExplicitDim a() As IntegerPrivate Sub Command1_Click() Dim i As Integer, j As Integer, n As Integer n = InputBox(数组行数n=, 输入, 5) ReDim a(n, 2) For i = 1 To n For j = 1 To 2 a(i, j) = Int(Rnd * 90) + 10 Picture1.Print a(i, j); Next j Picture1.Print Next iEnd SubPrivate Sub Command2_Click() Dim i As Integer, j As Integer Call sort(a) For i = 1 To UBound(a, 1) For j = 1 To 2 Picture2.Print a(i, j); Next j Picture2.Print Next iEnd SubPrivate Sub sort(a() As Integer) Dim i As Integer, j As Integer, t As Integer For i = 1 To UBound(a, 1) - 1 For j = i + 1 To UBound(a, 1) If a(i, 1) a(j, 1) Then t = a(i, 1): a(i, 1) = a(j, 1): a(j, 1) = t t = a(i, 2): a(i, 2) = a(j, 1): a(j, 2) = t ElseIf a(i, 1) = a(j, 1) And a(i, 2) a(j, 2) Then t = a(i, 1): a(i, 1) = a(j, 1): a(j, 1) = t t = a(i, 2): a(i, 2) = a(j, 1): a(j, 2) = t End If Next j Next iEnd Sub7、找出M、K(M100,K1000 And ( B=0 Or C10) Then Exit Function If A2+B2=C2 Then Fun1=TrueEnd FunctionPrivate Sub Command2_Click() Text1 = Text2 = list1.ClearEnd SubPrivate Sub Command3_Click() EndEnd Sub9、随机生成15个10100之间的整数,找出其中所有的素数以及最大的素数。Option Explicitprivate sub command1-click() Dim n, k As Integer Dim i As Integer, a(15) As Integer Randomize for i =1to15 a(i) = Int(Rnd * 90) + 10 text1= text1&str(a(i) If prime(a(i) Then list1.AddItem a(i) End If Next i Call max(k) text2 = kEnd SubPrivate Sub max(b As Integer) Dim i As Integer, n As Integer b = list1.List(0) For i = 1 To .ListCount - 1 If b list1.List(i) Then b = list1.List(i)Next iEnd Subprivate sub command2-click()text1 = text2 = list1.ClearEnd Subprivate sub command3-click()EndEnd Sub10、找出10009999范围内的所有满足“各位数字的四次方之和等于它本身”的四位数。.Option Explicitprivate sub command1-click() Dim m As long,n as long,i as long,j as integer,s as string m =text1 n=text2 fori =m to n if pd(i)then s=s&i&= for j =1 to len(cstr(i)-1 s= s& mid(cstr(i),j,1)&4+ next i s= s& mid(cstr(i),j,1)&4&vbcrlf end if next iend subprivate function pd (n as long) as boolean Dim i As Integer, sum As long for i=1 to len(cstr(n),i,1)4 next i if n =sumthen pd =true else pd= flase end ifend functionprivate sub command2-click()text1 = text2 = text3 = text1.setfocusend subprivate sub command3-click()end end sub11、找出介于A、B之间的所有只有素数因子(1和本身除外)的数。Option ExplicitPrivate Sub Command1_Click() Dim n As Integer, s As String Dim a As Integer, b As String a = Text1. Text: b = Text2. Text For n = a To b s = If findFactor(n,s) Then List1.AddItem n & 的素数因子是: & Left(s,Len(s) -1) Next nEnd SubPrivate Function findFactor(By Val n As Integer, h As String) As Boolean Dim i As Integer If prime(n) Then Exit Function For i = 2 To n/2 If n Mod i = 0 Then i是n的因子 If prime(i) Then i是素数 h = h & Str(i) & , Else Exit Function End if End if Next i If h Then findFactor = TrueEnd FunctionPrivate Function prime(n As Integer) As Boolean Dim i As Integer For i = 2 To sqr(n) If n Mod i = 0 Then Exit Function Next i prime = TureEnd FunctionPrivate Sub Command2_Click() Text1.Text = : Text2.Text = List1.Clear Text1.SetFocusEnd SubPrivate Sub Command3_Click() EndEnd Sub12、找出指定位数的对称素数位数n可以指定为3、4、5。option explicitprivate sub command1_click() dim i as long,n as tnteger,js as integer n=text1.textfor i=10(n-1) to 10n-1 if prime(i) And symmetry(i) then list1.additem i js = js+1 end if if js=0 then list1.additem 无对称素数end subprivate function prime(n as long)as boolean dim i as integer for i=2 to sqr(n) if n mod i =0 then exit function next i prime=tureend functionprivate function symmetry( n as long) a boolean dim s as string, st as string dim ias integer s=cstr(n) for i =1 to len(s) st = mid(s,i,1)&st next i if s=st then symmetry =tureend functionprivate sub command2_click() text1.text= list1.clear text1.setfocusend subprivate sub command3_click() endend sub13、对于任意输入的一个大于10的偶数N找出最靠近N的素数。假设K、M是接近N的两个素数,且KN,NM;若K-NN-M,则M是最靠近N的素数;否则K和M都是最靠近的素数N。Option ExplicitPrivate Sub Command1_Click() Dim N As Integer,K As Integer,M As Integer N=Text1 K=N+1: M=N-1 Do If Not Prime(K) Then K=K+2 End If Loop Until Prime(K) Do If Not Prime(M) Then M=M-2 End If Loop Until Prime(M) If K-NN-M Then Text2=M Else Text2=M &与 & K End IfEnd SubPrivate Function Prime(N As Integer) As Boolean Dim K As Integer For K=2 To Sqr(N) If N Mod k=0 Then Prime=False Exit Function End If Next K Prime=TrueEnd FunctionPrivate Sub Command2_Click() Text=: Text2= Text1.SetFocusEnd SubPrivate Sub Command3_Click() EndEnd Sub 14、生成一个n行n列由两位随机整数组成的二维数组(视为n阶方阵),找出主对角线上的最小元素和副对角线上的最大元素。Option ExplicitOption Base 1Dim A() As IntegerPrivate Sub Command1_Click() Dim i As Integer,J As Integer,n As Integer Randomize n=InputBox(n=,生成二维数组,5) ReDim A(n,n) For i=1 TO n For J=1 To n A(i,J) =Int(Rnd*90)+10 Picture1.Print Str(A(i,J); Next J Picture1.Print Next iEnd SubPrivate Sub Sub1(A() As Integer,R1 As Integer,R2 As Integer) Dim i As Integer,J As Integer,n As Integer Dim Max As Integer,Min As Integer n=UBound(A,1) Min=A(1,1): R1=i Max=A(1,n): R2=n Fori=2 To n If A(i,i)Max Then Max=A(i,n+1-i) R2=n+1-i End If Next iEnd SubPrivate Sub Command2_Click() Dim R1 As Integer,R2 As Integer,n As Integer CALL SUB1(A,R1,R2) N = ubound(A,1) TEXT1 = “主对角线上最小元素” & vbcrlf text1 = text & “a(& r1 &,& r2 & ) = a(r1,r2) & vbcrlf text1 = text & 副对角线上最大元素 & vbcrlf text = text & a( & n+1-r2 &,& r2 &) = & a(n+1-r2,r2)end subprivate sub cmdmand3_click() picture1.cls text1 = end sub15、随机生成m行n列由1位非零整数组成的数组;并分别求出数组中所有周边元素之和与周边元素(内部)之和。(m、n由Inputbox函数输入,缺省值分别为4、5)option explicit option base 1 dim a() as integer n as integer m as integerprivate sub commond1 click() dim i as integer j as integer dim ext as integer ins as integer n=inputbox(数组行数m:,数组处理,4) m=inputbox(数组列数n:,数组处理,5) redim a(n m) for i=1to n forj=1to m a(i,j)=int(rnd*9)+1 picture1.print next i call comput(ext,ins) text1=ext test2=ins end sub private sub comput(ext as integer ,ins as integer) dim i as integer j as integer for i=1to n for j=1 to m if i=1 or j=1 or i=n or j= m then ext=ext+a(i,j)else ins=ins+a(i,j) end if next j next i end sub private sub commond 2-click() picture1.cls end sub private sub commond3-click()end end sub16、找出输入字符中ASCII代码值最大的字符,把它移动到原字符串的末尾,其他字符的排列顺序维持不变。private sub commond1 click() dim st1 as string k as integer st1=test1 call maxc(st1,k) st1=left(st1,k-1)&right(st1,len(st1)-k)&mid(st1,k,1) test2=st1end sub private sub maxc (st as string k as integer) dim i as integer dim m as string m =left(st,1) k=1 for i=2 to len(st) if m=A and p=F then n=asc(p)-55else n=val(p)end if h2d = h2d+n*16kk=k-1 next iend functionprivate sub command2_click()text1=: text2=text1.setfocusend subprivate sub command3_click()endend sub18、输入一个已按字母顺序从小到大排列好的字符串,再输入1个任意字符,若字母串中已含有该字符,则将字符串中的这个字符删除,否则,将该字符按序插入到字符串的适应位置。Option explicitPrivate Sub Commond1_click() dim st1 as integer, p as string*1 dim n as integer st1=text1 p=text2 n=instr(st1,p) if n0 then text3=left(st1,n-1) & right(st1,len(st1)-n) else text3=change(st1,p) end ifend subprivate function change(s as integer,p as integer) as string dim i as integer, st as string for i=1 to len(s) st=mid(s,i,1) if asc(p)fb then fb=d:k=i end if next i text1=pn(k0 & - - - & pn(k+1)end subprivate function prime(n s integer)as boolean dim i as integer for i=1 to sqr(n) if n mod i=0 then exit function next i prime = trueend functionprivate sub commond2_click() picture1.cls text1=end subprivate sub commond3_() endend sub 20、验证任意末尾不为0的整数,与其反序数相加,若和数不是回文数,则再将该和数与其反序数相加,如此反复,最终的和数必为回文数。option explicitprivate sub commond1_click() dim n as integer, m as long, st as string n=text1 if right(cstr(n),1)0 then do m=fx(n) st=n & + & m & = & n+m list1.additem st n=n+m loop until hw(n) list1.additem验证成功 else msgbox数据非法 text1= text1.setfocus exit sub end ifend subprivate function fx(byval n as long) as long dim st as string do st=st & cstr(n mod 10) n=n10 loop until n=0 fx=val(st)end functionprivate function hm( n as long) as boolean if n=fx(n) then hw =tureend functionprivate sub commond2_click() text1= text1.setfocusend subprivate sub command3_click() endend sub 21、找出整数nm之间的所有互满数对。option explicitprivate sub command1_click() dim x as integer, y as integer, n as integer, m as integer dim sum as integer n =text1: m=text2 for x =n to m call sum_f(x,y) call sum_f(y,sum) if x=sum and xy then list1.additem( & x & , & y & ) end if next x if list1.listcount=0 then list1.additem无互满数对end subprivate sub sum_f(n as integer, sum as integer) dim i as integer sum = 0 for i= 1 to n/2 if n mod i=0 then sum=sum+1 end if next iend subprivate sub command2_click() text1= text2= list1.clear text1.setfocusend subprivate sub command3_cilck() endend sub22、某数列的前两项为2和7,其后继项根据当前的最后两项的乘积2 7 1 4 4 1 6 6 3 6,生成一个n项数列,显示在文本框中。option explicitoption base1private sub command1_click() dim n as integer,a()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国广电齐齐哈尔市2025秋招半结构化面试模拟30问及答案
- 白银市中石化2025秋招笔试模拟题含答案新材料与新能源岗
- 平凉市中石化2025秋招笔试模拟题含答案油品分析质检岗
- 昌吉回族自治州中石化2025秋招面试半结构化模拟题及答案油田工程技术岗
- 中国移动济宁市2025秋招网申填写模板含开放题范文
- 黄南藏族自治州中储粮2025秋招面试专业追问题库战略研究博士岗
- 博尔塔拉自治州中石化2025秋招笔试模拟题含答案市场营销与国际贸易岗
- 大唐电力内蒙古2025秋招计算机与信息专业面试追问及参考回答
- 中国移动石家庄市2025秋招计算机类专业追问清单及参考回答
- 2025年电动三轮车考试题及答案
- 董关鹏-沈阳课件
- 大学生活从“心”开始
- 企业消防安全基础知识培训讲义课件
- 淄博市2020年度专业技术人员继续教育公需课考试题及答案
- 大运河前世今生课件
- 商务英语翻译实务完整版教学ppt课件全套教程
- 第五章__大数定律与中心极限定理
- 现代控制理论教案Word版
- 基本建设项目管理办法
- 加弹机操作规程
- 国家开放大学《电气传动与调速系统》章节测试参考答案
评论
0/150
提交评论