delphi基本算法.doc
上传人:sy****28 上传时间:2024-09-13 格式:DOC 页数:4 大小:63KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

delphi基本算法.doc

delphi基本算法.doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

delphi算法:DELPHI基本算法疯狂代码http://CrazyCoder.cn/:http:/CrazyCoder.cn/Delphi/Article11679.html1.数论算法求两数最大公约数functiongcdab:eger:egerbeginb0thengcd:agcd:gcdbamodBend求两数最小公倍数functionlcmab:eger:egerbeginabthenswapaBlcm:awhilelcmmodb0doinclcmaend素数求法A.小范围内判断个数是否为质数:functionprimen:eger:BooleanvarI:egerbeginforI:2totruncsqrtndonmodI0thenbeginprime:falseexitendprime:trueendB.判断long范围内数是否为素数包含求50000以内素数表:proceduregetprimevarij:longp:.gif/1..50000ofbooleanbeginfillcharpptruep1:falsei:2whilei50000dobeginpithenbeginj:i2whilej50000dobeginpj:falseincjiendendinciendl:0fori:1to50000dopithenbegininclprl:iendendgetprimefunctionprimex:long:egervari:egerbeginprime:falsefori:1toldoprixthenxmodpri0thenexitprime:trueendprime2.3.4.求最小生成树A.Prim算法:procedureprimv0:egervarlowcostclosest:.gif/1..maxnofegerijkmin:egerbeginfori:1tondobeginlowcosti:costv0iclosesti:v0endfori:1ton-1dobegin寻找离生成树最近未加入顶点kmin:maxlongforj:1tondolowcostjminandlowcostj0thenbeginmin:lowcostjk:jendlowcostk:0将顶点k加入生成树生成树中增加条新边k到closestk修正各点lowcost和closest值forj:1tondocostkjlwocostjthenbeginlowcostj:costkjclosestj:kendendendprimB.Kruskal算法:贪心按权值递增顺序删去图中边若不形成回路则将此边加入最小生成树functionfindv:eger:eger返回顶点v所在集合vari:egerbegini:1whileinandnotvinvidoinciinthenfind:ifind:0endprocedurekruskalvartotij:egerbeginfori:1tondovi:i化定义n个集合第I个集合包含个元素Ip:n-1q:1tot:0p为尚待加入边数q为边集指针sort对所有边按权值递增排序存于eI中eI.v1和eI.v2为边I所连接两个顶点序号eI.len为第I条边长度whilep0dobegini:findeq.v1j:findeq.v2ijthenbegininctoteq.lenvi:vivjvj:decpendincqendwritelntotend5.最短路径A.标号法求解单源点最短路径:vara:.gif/1..maxn1..maxnofegerb:.gif/1..maxnofegerbi指顶点i到源点最短路径Pagemark:.gif/1..maxnofbooleanprocedurebhfvarbestbest_j:egerbeginfillcharmarkmarkfalsemark1:trueb1:01为源点repeatbest:0fori:1tondoIfmarkithen对每个已计算出最短路径点forj:1tondonotmarkjandaij0thenbest0orbiaijbestthenbeginbest:biaijbest_j:jendbest0thenbeginbbest_j:best;markbest_j:trueenduntilbest0endbhfB.Floyed算法求解所有顶点对的间最短路径:procedurefloyedbeginforI:1tondoforj:1tondoaIj0thenpIj:IpIj:0pIj表示I到j最短路径上j前驱结点fork:1tondo枚举中间结点fori:1tondoforj:1tondoaikajkaijthenbeginaij:aika