VB二级期末编程题.docx

上传人:太** 文档编号:86416033 上传时间:2023-04-14 格式:DOCX 页数:4 大小:12.91KB
返回 下载 相关 举报
VB二级期末编程题.docx_第1页
第1页 / 共4页
VB二级期末编程题.docx_第2页
第2页 / 共4页
点击查看更多>>
资源描述

《VB二级期末编程题.docx》由会员分享,可在线阅读,更多相关《VB二级期末编程题.docx(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、一、一维数组求和:Private Sub Commandl Click ()Dim a(10) As Integer, i As Integer, s As Integer RandomizeFor i = 1 To 10a(i) = Int (Rnd * 100 + 1)Textl.Text = Textl.Text & a(i) & Next iCall sum(a, s)Text2.Text = CStr(s)End SubPublic Sub sum(a() As Integer, s As Integer)Dim i As IntegerFor i = 1 To UBound(a)

2、s = s + a(i)Next iEnd Sub二、素数.找出100以内的挛生素数Private Sub Command1 Click()Dim i As IntegerFor i = 2 To 97If prime(i)= True And prime(i + 2)= True Then Listl. Additem i & 和 & i + 2End IfNext iEnd SubPublic Function prime(n As Integer) As BooleanDim i As IntegerFor i = 2 To sqr (n)If n Mod i = 0 Then Exit

3、 ForNext iIf i n - 1 Then(If i Len(CStr (i) Then Listl. Addltem i End IfNext iEnd SubPublic Function prime(n As Integer) As BooleanDim i As IntegerFor i = 2 To sqr (n)If n Mod i = 0 Then Exit ForNext iIf i nIf i nIf i n-1 Then=Trueprime End If End Function 三、逆序输出L求逆序Private Sub Commandl_Click()Dim a

4、 As Stringa = Textl. TextText2.Text 二 reserve(a)End SubPublic Function reserve(n As String) As String Dim i As Integer, b As StringFor i = 1 To Len (CStr(n) b = Mid(n, i, 1) & bNext i reserve 二 bEnd Function1 .找出所有四位回文数Private Sub Command1 Click ()Dim num As Integer, renum As IntegerFor num = 1000 T

5、o 9999Call reserve(num, renum)If num = renum ThenListl. Addltem CStr(num)End IfNext numEnd SubPublic Sub reserve(ByVai n As Integer, ren As Integer) Dim st As String, i As IntegerFor i = 1 To Len(CStr (n)st = Mid (CStr(n), i, 1) & stNext iren = Vai(st)End Sub四、求阶乘之和L非递归Private Sub Commandl Click()Di

6、m i As Integer, n As Integer, s As Long n = Vai(Textl. Text)For i = 1 To ns = s + fact(i)Next iText2.Text = CStr (s)End SubPublic Function fact(ByVai k As Integer) As Dim i As Integer fact = 1 For i = 1 To k fact = fact * i Next iEnd Function2 .递归Private Sub Commandl Click ()Dim i As Integer, n As I

7、nteger, s As n = Vai (Textl.Text)For i = 1 To ns = s + fact (i)Next iText2.Text = CStr (s)End SubPublic Function fact(ByVai k As Integer) As Long If k = 0 Or k = 1 Then fact = 1Elsefact = k * fact (k - 1)End IfEnd Function五、求最小公倍数(先求最大公约数) Private Sub Commandl_Click ()Dim m As Integer, n As Integer

8、m = Vai(Textl. Text) n = Vai(Text2. Text)Text3. Text = CStr (m * n / gcd(m, n) End SubPublic Function gcd(ByVai m As Integer, ByVai n As Integer) As Integer Dim r As Integer Do r = m Mod n m = n n = rLoop Until r = 0 gcd 二 mEnd Function六、升、降序数判断L升序数Private Sub Command1_Click()Dim i As IntegerFor i =

9、 100 To 999If sx (i) Then Listl. Additem iNext iEnd SubPublic Function sx(n As Integer) As Boolean Dim i As Integer, s As String s = CStr (n) sx = False可省略For i = 1 To Len(s) - 1If Mid(s, i, 1) = Mid(s, i + 1, 1) Then Exit Function Next i sx = TrueEnd Function2)降序数Private Sub Command1 Click()Dim i A

10、s IntegerFor i = 100 To 999If jx(i)= True Then Listl. Additem iNext iEnd SubPublic Function jx(n As Integer) As Boolean Dim i As Integer, s As String s = CStr (n) jx = False 可以省略 For i = 1 To Len (s) - 1If Mid(s, i, 1) = Mid(s, i + 1, 1) Then Exit Function Next i jx 二 TrueEnd Function七、求因子和Private S

11、ub Commandl Click ()Dim n As Integer n = Vai(Textl. Text) Text2.Text = CStr(sum(n)End SubPublic Function sum(n As Integer) As Integer Dim i As IntegerFor i = 1 To nIf n Mod i = 0 Then sum = sum + i Next iEnd Function八、求因子个数Private Sub Command1 Click()Dim n As Integer n 二 Vai (Textl.Text)Text2.Text = CStr(num (n)End SubPublic Function num(n As Integer) As Integer Dim i As Integer, k As Integer For i = 1 To nIf n Mod i = 0 Then k = k + 1Next i num = k End Function

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁