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

上传人:b****5 文档编号:12371497 上传时间:2023-04-18 格式:DOCX 页数:20 大小:26.89KB
下载 相关 举报
江苏省计算机等级考试常考算法过程集ma3.docx_第1页
第1页 / 共20页
江苏省计算机等级考试常考算法过程集ma3.docx_第2页
第2页 / 共20页
江苏省计算机等级考试常考算法过程集ma3.docx_第3页
第3页 / 共20页
江苏省计算机等级考试常考算法过程集ma3.docx_第4页
第4页 / 共20页
江苏省计算机等级考试常考算法过程集ma3.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

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

《江苏省计算机等级考试常考算法过程集ma3.docx》由会员分享,可在线阅读,更多相关《江苏省计算机等级考试常考算法过程集ma3.docx(20页珍藏版)》请在冰豆网上搜索。

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

江苏省计算机等级考试常考算法过程集ma3

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

1、判断一个数是否为素数的算法

素数(质数):

就是一个大于等于2的整数,并且只能被1和本身整除,而不能被其他整数整除的数。

对于m,从I=2,3,4,……,m-1依次判别能否被I整除,只要有一个能整除,m就不是素数,否则m是素数。

方法一:

PrivateFunctionprime(ByValnAsLong)AsBoolean

DimiAsLong

Fori=2Ton-1

If(nModi)=0ThenExitFor

NextI

IfI=nthenprime=True

EndFunction

方法二:

PrivateFunctionPrime(nAsInteger)AsBoolean

DimiAsInteger

Fori=2ToSqr(n)

IfnModi=0ThenExitFunction

Nexti

Prime=True

EndFunction

说明:

n是要判断的数,当n为素数时函数返回值为True,当n为不是素数时函数返回值为False。

2、求解n以内的所有素数并放入数组A中。

(P104,例6-4;P162,例7-12)

PrivateSubprime(nAsInteger,a()AsInteger)

DimiAsInteger,jAsInteger,idxAsInteger

Fori=2Ton‘例如n=10

Forj=2ToSqr(i)‘判断素数

IfiModj=0ThenExitFor

Nextj

Ifj>Sqr(i)Then

idx=idx+1

ReDimPreservea(idx)

a(idx)=i

EndIf

Nexti

EndSub

说明:

n以内所有的素数均放入数组A中。

3、求最大公约数

用辗转相除法求两自然数m、n的最大公约数。

(1)首先,对于已知两数m、n,比较并使得m>n;

(2)m除以n得余数r;

(3)若r=0,则n为求得的最大公约数,算法结束;否则执行步骤(4)

(4)mnnr再重复执行

(2)。

例如:

24与9

分析步骤:

m=24,n=9

r=mmodn=6;r≠0m=9n=6

r=mmodn=3;r≠0m=6n=3

r=mmodn=0;所以n(n=3)为最大公约数。

算法实现方法一:

(循环实现)辗转相除法(欧几里德算法)P68

PrivateFunctionGCD(ByValmAsLong,ByValnAsLong)AsLong

DimtempAsLong

Ifm

m=n:

n=temp‘交换

DimrAsLong

Do

r=mModn

Ifr=0ThenExitDo

m=n

n=r

Loop

GCD=n

EndFunction

方法二:

递归过程P134/162

PrivateFunctionGcd(ByValmAsLong,ByValnasLong)

DimrAsLong

R=mmodn

Ifr=0then

Gcd=n

Else

m=n:

n=r

Gcd=Gcd(m,n)

Endif

EndFunction

4、最小公倍数

方法一:

m×n÷最大公约数

PrivateFunctionLCM(ByValaAsInteger,ByValbAsInteger)AsLong

DimrAsLong,mAsLong

m=a*b

r=aModb

DoWhiler<>0

a=b

b=r

r=aModb

Loop

LCM=m/b

EndFunction

方法二:

是用a的n倍去除b,当能够整除时,则n*a为所求最小公倍数。

P152例7-5

PrivateFunctionLCM(ByValaAsInteger,ByValbAsInteger)asLong

DimMAsLong,FlgAsBoolean

Flg=False

DoUntilFlg

M=M+a

IfMModb=0Then

Flg=True

EndIf

Loop

LCM=M

EndFunction

或可写成如下形式:

PrivateFunctionLCM(ByValaAsInteger,ByValbAsInteger)asLong

DimIAsLong

ForI=aToa*bstepa

IfImodb=0Then

LCM=I

ExitFunction

EndIf

NextI

EndFunction

5、互质数P106

最大公约数为1的两个正整数。

(判断一对整数是否是互质数。

PrivateFunctionmutual_prime(n1AsLong,n2AsLong)AsBoolean

DimminAsLong,kAsLong

Ifn1

Fork=2Tomin/2

Ifn1Modk=0Andn2Modk=0ThenExitFunction

Nextk

mutual_prime=True

EndFunction

6、求一整数的所有因子P133

'一个整数n的因子指能使n被整除的所有整数,因子包含1和它自身。

'真因子包括1,但不包括本身。

'例如:

12的因子为1,2,3,4,6,12,而其真因子为1,2,3,4,6。

PrivateSubfactor(xAsInteger,fn()AsInteger)

DimiAsInteger,nAsInteger

ReDimfn

(1):

n=1:

fn

(1)=1或写成:

n=0

Fori=2Tox/2‘或2tox-1

IfxModi=0Then

n=n+1

ReDimPreservefn(n)

fn(n)=i

EndIf

Nexti

EndSub

7、求一整数的所有因子的和。

PrivateFunctionSum_Factor(NAsInteger)AsLong

DimiAsInteger,jAsInteger,sAsInteger

DoWhilei

i=i+1

IfNModi=0Then

s=s+i

EndIf

Loop

Sum_Factor=s

EndFunction

8、求n!

的阶乘

'第一种方法采用递归过程加以实现P144

PrivateFunctionfact(ByValnAsInteger)AsLong

Ifn<=1Then

fact=1

Else

fact=n*fact(n-1)

EndIf

EndFunction

'第二种方法采用累乘积的方法加以实现P131

PrivateFunctionfact(ByValnAsInteger)AsLong

DimiAsInteger

fact=1

Fori=1Ton

fact=fact*i

Nexti

EndFunction

9、判断一个数是否为降序数。

'降序数就是从高位到低位逐渐减小,即高位数一定大于低位数。

PrivateFunctionorder(sAsInteger)AsBoolean

Dimb()AsInteger,nAsInteger,iAsInteger

n=Len(CStr(s))

Fori=1Ton

ReDimPreserveb(i)

b(i)=sMod10

s=s\10

Nexti

Fori=1Ton-1

Ifb(i)>b(i+1)ThenExitFunction

Nexti

order=True

EndFunction

注:

当把程序中的“>”改成“<”时,可判断一个数是否为升序数。

10、求反序数

PrivateFunctionreverse_num(ByValnAsLong)AsLong

Do

reverse_num=reverse_num*10+(nMod10)

n=n\10

LoopWhilen>0

EndFunction

11、十进制正整数向r进制转换P158

PrivateFunctionTran(ByValmAsInteger,ByValrAsInteger)AsString

DimStrDtoRAsString,nAsInteger

DoWhilem<>0

n=mModr

m=m\r

Ifn>9Then

StrDtoR=Chr(65+n-10)&StrDtoR

Else

StrDtoR=n&StrDtoR

EndIf

Loop

Tran=StrDtoR

EndFunction

说明:

m是要转换的十进制数,函数返回值是转换成的r进制数。

12、r进制正整数向十进制转换

PrivateFunctionTran(ByValsAsString,ByValrAsInteger)AsInteger

DimnAsInteger,decAsInteger

DimiAsInteger

s=UCase(Trim(s))

Fori=1ToLen(s)

IfMid(s,i,1)>="A"Then

n=Asc(Mid(s,i,1))-Asc("A")+10

Else

n=Val(Mid(s,i,1))

EndIf

dec=dec+n*r^(Len(s)-i)

Nexti

Tran=dec

EndFunction

13、排序算法

'直接选择法排序P106

PrivateSubchoose(a()AsInteger)'本过程排序结果为从小到大

DimiAsInteger,jAsInteger

DimtempAsInteger

Fori=1ToUBound(a)-1

Forj=i+1ToUBound(a)

Ifa(i)>a(j)Then

temp=a(i)

a(i)=a(j)

a(j)=temp

EndIf

Nextj

Nexti

EndSub

'改进的选择法排序P107

PrivateSubnsort(sort()AsInteger)'本过程排序结果为从大到小

DimiAsInteger,jAsInteger

DimtempAsInteger

DimpointerAsInteger'记录每一轮的最小值下标,可理解为指针,用于指向最小值

Fori=1ToUBound(sort)-1

pointer=i

Forj=i+1ToUBound(sort)

Ifsort(pointer)

pointer=j

EndIf

Nextj

Ifi<>pointerThen

temp=sort(m)'一轮结束,m指向本轮次最小值,

sort(m)=sort(i)'把m指向元素与本轮次第一个元素进行交换

sort(i)=temp

EndIf

Nexti

EndSub

说明:

Sub子过程choose及nsort的功能一样,都是对形参数组进行排序,当子过程运行结束时,对应的实参数组即完成排序工作。

改进后的选择排序算法减少了数据交换的次数,提高了运行效率。

'冒泡法排序P157

PrivateSubBubble_Sort(Sort()AsInteger)

DimIAsInteger,JAsInteger,TemAsInteger

DimUbAsInteger

Ub=UBound(Sort)

ForI=1ToUb-1

ForJ=1ToUb-I

IfSort(J)>Sort(J+1)Then'>为升序,<为降序

Tem=Sort(J)

Sort(J)=Sort(J+1)

Sort(J+1)=Tem

EndIf

NextJ

NextI

EndSub

'改进的冒泡法排序P158

PrivateSubBubble_Sort(Sort()AsInteger)

DimIAsInteger,TemAsInteger

DimUbAsInteger,SwitchAsBoolean

Ub=UBound(Sort)

Switch=True

DoWhileSwitch

Switch=False

Ub=Ub-1'最大下标依次减1

ForI=1ToUb

IfSort(I)>Sort(I+1)Then

Switch=True

Tem=Sort(I)

Sort(I)=Sort(I+1)

Sort(I+1)=Tem

EndIf

NextI

Loop

EndSub

'直接插入排序(升序)P164

PrivateSubInsertion(Sort()AsInteger)

DimKAsInteger,IAsInteger,TempAsInteger

DimUbAsInteger

Ub=UBound(Sort)

ForI=2ToUb

Temp=Sort(I)

K=I-1

DoWhileTemp

Sort(K+1)=Sort(K)

K=K-1

IfK<=0ThenExitDo

Loop

Sort(K+1)=Temp

NextI

EndSub

说明:

函数的功能是使用直接插入排序算法对形参数组A进行排序(升序)。

14、顺序法查找P109

PrivateFunctionSequentialSearch(a()AsInteger,FindAsInteger,posAsInteger)AsBoolean

DimIAsInteger

ForI=1ToUBound(a)

Ifa(I)=FindThenExitFor

NextI

IfI<=UBound(a)Then

SequentialSearch=True

pos=I

Else

SequentialSearch=False

pos=-1

EndIf

EndFunction

说明:

函数的功能是在数组a中查找有无指定的数Find存在,如果存在则函数返回值为True,否则返回值为False。

另外,如果找到指定数,其在数列中的位置序号经形参pos返回。

15、二分法查找P109

PrivateFunctionBinarySearch(a()AsInteger,FindAsInteger,posAsInteger)AsBoolean

DimleftAsInteger,rightAsInteger,midAsInteger

BinarySearch=False

pos=-1

left=LBound(a):

right=UBound(a)

DoWhileleft<=right

mid=(left+right)/2

Ifa(mid)=FindThen

BinarySearch=True

pos=mid

ExitFunction

ElseIfFind>a(mid)Then

left=mid+1

Else

right=right-1

EndIf

Loop

EndFunction

'说明:

函数功能是在数组a中查找指定数Find,找到时函数的返回值为True,否则返回False。

二分查找法每次查找后都将查找区间缩小一半,查找效率较高。

但必须将数组a事先排序。

16、删除一个数列中的重复数P114---P116

PrivateSubDelDuplication(a()AsInteger)

DimUbAsInteger,nAsInteger,iAsInteger,jAsInteger

Ub=UBound(a)

n=1

DoWhilen

i=n+1

DoWhilei<=Ub'内循环从第n个元素的下一个元素开始逐一与第i个元素比较

Ifa(n)=a(i)Then'如果存在重复元素

Forj=iToUb-1'重复元素后面的元素整体前移,覆盖掉重复元素

a(j)=a(j+1)

Nextj

Ub=Ub-1'元素个数-1

ReDimPreservea(Ub)'重定义数组,减少元素空间

Else

i=i+1'如果不存在重复元素,则继续向后查找

EndIf

Loop

n=n+1'继续对第n+1个元素进行查重处理

Loop

EndSub

说明:

函数的功能是删除数组A中的重复数。

当函数运行结束时,形参数组中已经无重复数。

17、求解一维数组中的最大值、最小值P97

PrivateSubMaxMin(a()AsSingle,maxAsSingle,minAsSingle)

DimiAsInteger

max=a

(1):

min=a

(1)

Fori=2ToUBound(a)

Ifa(i)>maxThen

max=a(i)

ElseIfa(i)

min=a(i)

EndIf

Nexti

EndSub

说明:

数组A用于存放一组数,参数max返回该组数中的最大值,参数min返回该组数中的最小值。

18、利用牛顿迭代法求解一元超越方程。

P78

假设需要求解的方程为:

xex-1=0

牛顿迭代公式为:

xn+1=xn-f(xn)/f’(xn),n=0,1,2,3…

PrivateFunctionDieDai(ByValxAsSingle,ByValepsAsSingle)AsSingle

Dimx1AsSingle

Do

x1=x

x=x1-(x1*Exp(x1)-1)/(Exp(x1)*(x1+1))

LoopUntilAbs(x-x1)<=eps

DieDai=x

EndFunction

说明:

x是方程的初始根,Eps是允许的误差值,函数的返回值是方程在误差范围内的近似根。

19、利用级数法编程求解arcsinx的函数值

PrivateFunctionafun(ByValxAsSingle,ByValnAsInteger)AsSingle

DimiAsInteger,pAsSingle

p=1

Fori=1Ton

p=p*(2*i-1)/(2*i)

Nexti

afun=p*x^(2*n+1)/(2*n+1)

EndFunction

PrivateFunctionarcsinx(ByValxAsSingle,ByValepsAsSingle)AsSingle

DimsAsSingle,nAsInteger,aAsSingle

s=x:

n=1

Do

a=afun(x,n)

Ifa<=epsThenExitDo

s=s+a

n=n+1

Loop

arcsinx=s

EndFunction

说明:

函数afun用于求解级数第n项的值,函数arcsinx用于求解反正弦函数的值,参数Eps是允许的误差值。

20、分解数字P77-78水仙花数

PrivateSubdvnum(ByValmAsLong,a()AsInteger)

DimiAsInteger,lengthAsInteger

length=Len(CStr(m))

ReDima(length)

Fori=1Tolength

a(i)=mMod10

m=m\10

Nexti

EndSub

21、验证回文数

"回文数"是一种数字。

如:

98789,这个数字正读是98789,倒读也是98789,正读倒读一样,所以这个数字就是回文数。

PrivateFunctionfw(ByValnAsLong)AsBoolean

Dimlen1AsInteger,flgAsBoolean,iAsInteger,str1AsString

str1=CStr(n)

len1=Len(str1)

Fori=1Tolen1/2

IfMid(str1,i,1)<>Mid(str1,

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

当前位置:首页 > 自然科学 > 天文地理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1