收藏 分享(赏)

江苏省计算机等级考试常考算法过程集2010.doc

上传人:gnk289057 文档编号:4684572 上传时间:2019-01-07 格式:DOC 页数:14 大小:74KB
下载 相关 举报
江苏省计算机等级考试常考算法过程集2010.doc_第1页
第1页 / 共14页
江苏省计算机等级考试常考算法过程集2010.doc_第2页
第2页 / 共14页
江苏省计算机等级考试常考算法过程集2010.doc_第3页
第3页 / 共14页
江苏省计算机等级考试常考算法过程集2010.doc_第4页
第4页 / 共14页
江苏省计算机等级考试常考算法过程集2010.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

1、1二、编程题(26 分)【题目】编写程序,求出巴都万数列(Padovan Sequence)的第 n1 项到第 n2 项中的素数数据项。巴都万数列的计算公式如下:P = 【编程要求】1程序参考界面如图所示,编程时不得增加或减少界面对象或改变对象的种类,窗体及界面元素大小适中,且均可见。2运行程序,在两个文本框中分别输入起始项与终止项的序号后按“查找“ 按钮,则在多行文本框中,按每行 10 个元素输出数列项数据,并在列表框中输出其中的素数数据项及其序号;按“清除 “按钮,将三个文本框与列表框清空,焦点置于第一个文本框上;按“结束“ 按扭,结束程序运行。3程序中应定义一个判断素数的通用过程。Opt

2、ion ExplicitPrivate Sub CmdFind_Click()Dim n1 As Integer, n2 As Integer, i As IntegerDim k As Integer, js As Integer, st As Stringn1 = Text1: n2 = Text2For i = n1 To n2k = Padovan(i)st = st 并分别求出数组中所有周边元素之和与非周边元素(内部 )之和。(m、n 由 InputBox 函数输入,缺省值分别为 4、5)【编程要求】1程序参考界面如图所示,编程时不得增加或减少界面对象或改变对象的种类,窗体及界面元素

3、大小适中,且均可见;2运行程序,按“运行“ 按钮 ,出现如图的 InputBox 函数窗口,分别输入数组行、列数( 或使用缺省值)后按“ 确定“,则生成随机数组并显示在图片框中,并在文本框 1 与 2 中分别输出数组周边元素之和与非周边元素(内部) 之和;按“清除“按钮 ,将所有文本框和图片框清空;按“结束“ 按钮,结束程序运行; 3程序中应定义一个通用过程,用于求二维数组的周边元素之和与非周边元素之和。(1)Loop Until k3 改为 Loop Until k=3(2)Text2=Text2 Next jPicture1.PrintNext iCall comput(ext, ins)

4、Text1 = extText2 = insEnd SubPrivate Sub comput(ext As Integer, ins As Integer)Dim i As Integer, j As IntegerFor i = 1 To nFor j = 1 To mIf i = 1 Or j = 1 Or i = n Or j = m Thenext = ext + a(i, j)Elseins = ins + a(i, j)End IfNext jNext iEnd SubPrivate Sub Command2_Click()Picture1.ClsText1 = “Text2 =

5、 “4End SubPrivate Sub Command3_Click()EndEnd Sub江苏省计算机等级考试常考算法过程集1.求一整数的所有因子一个整数 n 的因子指能使 n 被整除的所有整数,因子包含 1 和它自身。真因子则不包括 1 及其自身。例如:12 的因子为 1,2,3,4,6,12,而其真因子为 2,3,4,6。求给定数的所有因子Private Sub factor(x As Integer, fn() As Integer)Dim i As Integer, n As IntegerReDim fn(1): n = 1: fn(1) = 1For i = 2 To x /

6、 2If x Mod i = 0 Thenn = n + 1ReDim Preserve fn(n)fn(n) = iEnd IfNext iEnd Sub2.求一整数的所有因子的和Private Function Sum_Factor(N As Integer) As LongDim i As Integer, j As Integer, s As IntegerDo While i Sqr(i) Thenidx = idx + 1ReDim Preserve a(idx)a(idx) = iEnd IfNext iEnd Sub说明:n 以内所有的素数均放入数组 A 中。5.求最大公约数递

7、归方法求最大公约数Private Function Gcd(ByVal m As Long, ByVal n As Long) as LongDim R As LongR = m Mod nIf R = 0 ThenGcd = nElsem = nn = RGcd = Gcd(m, n)End IfEnd Function辗转相除法(欧几里德算法)求两个自然数的最大公约数(普通方法)Private Function Gcd(ByVal a As Long, ByVal b As Long) As LongDim R As LongR = a Mod bDo While R 0a = bb =

8、rr = a Mod bLoopLCM = m / bEnd Function7.求除乘第一种方法采用递归过程加以实现Private Function fact(n As Integer) As LongIf n b(i + 1) Then Exit FunctionNext iorder = TrueEnd Function注:当把程序中的“”改成“ Mid(str1, len1 - i + 1, 1) Thenflg = FalseExit FunctionEnd IfNext ifw = TrueEnd Function类似问题:判断一个字符串是否为回文串9Private Functio

9、n HW(str As String) As BooleanDim i As Integer, L As IntegerL = Len(str)HW = FalseFor i = 1 To L 2If Mid(str, i, 1) 0End Function14.十进制正整数向 r 进制转换Private Function Tran(ByVal m As Integer, ByVal r As Integer) As StringDim StrDtoR As String, n As IntegerDo While m 9 ThenStrDtoR = Chr(65 + n - 10) & St

10、rDtoRElseStrDtoR = n & StrDtoREnd IfLoopTran = StrDtoREnd Function说明:m 是要转换的十进制数,函数返回值是转换成的 r 进制数。15. r 进制正整数向十进制转换Private Function Tran(ByVal s As String, ByVal r As Integer) As IntegerDim n As Integer, dec As IntegerDim i As Integers = UCase(Trim(s)For i = 1 To Len(s)If Mid(s, i, 1) = “A“ Thenn =

11、Asc(Mid(s, i, 1) - Asc(“A“) + 1010Elsen = Val(Mid(s, i, 1)End Ifdec = dec + n * r (Len(s) - i)Next iTran = decEnd Function16.排序算法改进的选择法排序Private Sub nsort(sort() As Integer) 本过程排序结果为从大到小Dim i As Integer, j As IntegerDim temp As Integer Dim pointer As Integer 记录每一轮的最小值下标,可理解为指针,用于指向最小值For i = 1 To UB

12、ound(sort) - 1pointer = iFor j = i + 1 To UBound(sort)If sort(pointer) pointer Then temp = sort(m) 一轮结束,m 指向本轮次最小值, sort(m) = sort(i) 把 m 指向元素与本轮次第一个元素进行交换sort(i) = tempEnd IfNext iEnd Sub说明:Sub 子过程 choose 及 nsort 的功能一样,都是对形参数组进行排序,当子过程运行结束时,对应的实参数组即完成排序工作。改进后的选择排序算法减少了数据交换的次数,提高了运行效率。冒泡法排序Private S

13、ub Bubble_Sort(Sort() As Integer)Dim I As Integer, J As Integer, Tem As IntegerDim Ub As IntegerUb = UBound(Sort)For I = 1 To Ub - 1For J = 1 To Ub - IIf Sort(J) Sort(J + 1) Then 为升序, Sort(I + 1) ThenSwitch = TrueTem = Sort(I)Sort(I) = Sort(I + 1)Sort(I + 1) = TemEnd IfNext ILoopEnd Sub直接插入排序(升序)Pri

14、vate Sub Insertion(Sort() As Integer)Dim K As Integer, I As Integer, Temp As IntegerDim Ub As IntegerUb = UBound(Sort)For I = 2 To UbTemp = Sort(I)K = I - 1Do While Temp a(mid) Thenleft = mid + 1Elseright = right - 1End IfLoopEnd Function说明:函数功能是在数组 a 中查找指定数 Find,找到时函数的返回值为 True,否则返回False。二分查找法每次查找后

15、都将查找区间缩小一半,查找效率较高。但必须将数组 a 事先排序。19.删除一个数列中的重复数Private Sub DelDuplication(a() As Integer)Dim Ub As Integer, n As Integer, i As Integer, j As IntegerUb = UBound(a)n = 113Do While n max Thenmax = a(i)ElseIf a(i) min Thenmin = a(i)End IfNext iEnd Sub说明:数组 A 用于存放一组数,参数 max 返回该组数中的最大值,参数 min 返回该组数中的最小值。21

16、.利用牛顿迭代法求解一元超越方程假设需要求解的方程为:xe x-1=0牛顿迭代公式为:x n+1=xn-f(xn)/f(xn) , n=0,1,2,3Private Function DieDai(ByVal x As Single, ByVal eps As Single) As SingleDim x1 As SingleDox1 = xx = x1 - (x1 * Exp(x1) - 1) / (Exp(x1) * (x1 + 1)Loop Until Abs(x - x1) = epsDieDai = x14End Function说明:x 是方程的初始根,Eps 是允许的误差值,函数

17、的返回值是方程在误差范围内的近似根。22.利用级数法编程求解 arcsinx 的函数值 ni iixx1 )12(753 *)(42364231x2xarcsinPrivate Function afun(ByVal x As Single, ByVal n As Integer) As SingleDim i As Integer, p As Singlep = 1For i = 1 To np = p * (2 * i - 1) / (2 * i)Next iafun = p * x (2 * n + 1) / (2 * n + 1)End FunctionPrivate Function arcsinx(ByVal x As Single, ByVal eps As Single) As SingleDim s As Single, n As Integer, a As Singles = x: n = 1Doa = afun(x, n)If a = eps Then Exit Dos = s + an = n + 1Looparcsinx = sEnd Function说明:函数 afun 用于求解级数第 n 项的值,函数 arcsinx 用于求解反正弦函数的值,参数Eps 是允许的误差值。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教育教学 > 计算机等级考试

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报