1、C C+算法实例C C+,算法实例一、 数论算法 1求两数的最大公约数 function gcd(a,b:integer):integer;begin if b=0 then gcd:=aelse gcd:=gcd (b,a mod b);end ; 2求两数的最小公倍数 function lcm(a,b:integer):integer;beginif a0 do inc(lcm,a);end; 3素数的求法 A.小范围内判断一个数是否为质数:function prime (n: integer): Boolean;var I: integer;beginfor I:=2 to trunc(
2、sqrt(n) doif n mod I=0 then begin prime:=false; exit;end;prime:=true;end; B.判断longint范围内的数是否为素数(包含求50000以内的素数表):procedure getprime;var i,j:longint;p:array1.50000 of boolean;beginfillchar(p,sizeof(p),true);p1:=false;i:=2;while i50000 do beginif pi then beginj:=i*2;while j=x then breakelse if x mod pr
3、i=0 then exit;prime:=true;end;prime 二、图论算法 1最小生成树 A.Prim算法:procedure prim(v0:integer);varlowcost,closest:array1.maxn of integer;i,j,k,min:integer;beginfor i:=1 to n do beginlowcosti:=costv0,i;closesti:=v0;end;for i:=1 to n-1 do begin寻找离生成树最近的未加入顶点kmin:=maxlongint;for j:=1 to n doif (lowcostjmin) and
4、 (lowcostj0) then beginmin:=lowcostj;k:=j;end;lowcostk:=0; 将顶点k加入生成树生成树中增加一条新的边k到closestk修正各点的lowcost和closest值for j:=1 to n doif costk,jlwocostj then beginlowcostj:=costk,j;closestj:=k;end;end;end;prim B.Kruskal算法:(贪心) 按权值递增顺序删去图中的边,若不形成回路则将此边加入最小生成树。function find(v:integer):integer; 返回顶点v所在的集合var i
5、:integer;begini:=1;while (i=n) and (not v in vseti) do inc(i);if i0 do begini:=find(eq.v1);j:=find(eq.v2);if ij then begininc(tot,eq.len);vseti:=vseti+vsetj;vsetj:=;dec(p);end;inc(q);end;writeln(tot);end; 2.最短路径 A.标号法求解单源点最短路径:vara:array1.maxn,1.maxn of integer;b:array1.maxn of integer; bi指顶点i到源点的最短
6、路径mark:array1.maxn of boolean; procedure bhf;varbest,best_j:integer;beginfillchar(mark,sizeof(mark),false);mark1:=true; b1:=0;1为源点repeatbest:=0;for i:=1 to n doIf marki then 对每一个已计算出最短路径的点for j:=1 to n doif (not markj) and (ai,j0) then if (best=0) or (bi+ai,j0 then beginbbest_j:=best;markbest_j:=tru
7、e;end;until best=0;end;bhf B.Floyed算法求解所有顶点对之间的最短路径:procedure floyed;beginfor I:=1 to n dofor j:=1 to n doif aI,j0 then pI,j:=I else pI,j:=0; pI,j表示I到j的最短路径上j的前驱结点for k:=1 to n do 枚举中间结点for i:=1 to n dofor j:=1 to n doif ai,k+aj,kai,j then beginai,j:=ai,k+ak,j;pI,j:=pk,j;end;end; C. Dijkstra 算法: var
8、a:array1.maxn,1.maxn of integer;b,pre:array1.maxn of integer; prei指最短路径上I的前驱结点mark:array1.maxn of boolean;procedure dijkstra(v0:integer);beginfillchar(mark,sizeof(mark),false);for i:=1 to n do begindi:=av0,i;if di0 then prei:=v0 else prei:=0;end;markv0:=true;repeat 每循环一次加入一个离1集合最近的结点并调整其他结点的参数min:=m
9、axint; u:=0; u记录离1集合最近的结点for i:=1 to n doif (not marki) and (dimin) then beginu:=i; min:=di;end;if u0 then beginmarku:=true; for i:=1 to n doif (not marki) and (au,i+dudi) then begindi:=au,i+du;prei:=u;end;end;until u=0;end; 3.计算图的传递闭包 Procedure Longlink;VarT:array1.maxn,1.maxn of boolean;BeginFillc
10、har(t,sizeof(t),false);For k:=1 to n doFor I:=1 to n doFor j:=1 to n do TI,j:=tI,j or (tI,k and tk,j);End; 4无向图的连通分量 A.深度优先procedure dfs ( now,color: integer);beginfor i:=1 to n doif anow,i and ci=0 then begin 对结点I染色ci:=color;dfs(I,color);end;end; B 宽度优先(种子染色法) 5关键路径 几个定义: 顶点1为源点,n为汇点。a. 顶点事件最早发生时间V
11、ej, Ve j = max Ve j + wI,j ,其中Ve (1) = 0;b. 顶点事件最晚发生时间 Vlj, Vl j = min Vlj wI,j ,其中 Vl(n) = Ve(n);c. 边活动最早开始时间 EeI, 若边I由表示,则EeI = Vej;d. 边活动最晚开始时间 ElI, 若边I由表示,则ElI = Vlk wj,k;若 Eej = Elj ,则活动j为关键活动,由关键活动组成的路径为关键路径。求解方法:a. 从源点起topsort,判断是否有回路并计算Ve;b. 从汇点起topsort,求Vl;c. 算Ee 和 El; 6拓扑排序 找入度为0的点,删去与其相连的
12、所有边,不断重复这一过程。例 寻找一数列,其中任意连续p项之和为正,任意q 项之和为负,若不存在则输出NO. 7.回路问题 Euler回路(DFS)定义:经过图的每条边仅一次的回路。(充要条件:图连同且无奇点) Hamilton回路定义:经过图的每个顶点仅一次的回路。 一笔画充要条件:图连通且奇点个数为0个或2个。 9判断图中是否有负权回路 Bellman-ford 算法 xI,yI,tI分别表示第I条边的起点,终点和权。共n个结点和m条边。procedure bellman-fordbeginfor I:=0 to n-1 do dI:=+infinitive;d0:=0;for I:=1
13、to n-1 dofor j:=1 to m do 枚举每一条边if dxj+tjdyj then dyj:=dxj+tj;for I:=1 to m doif dxj+tjdyj then return false else return true;end; 10第n最短路径问题 *第二最短路径:每举最短路径上的每条边,每次删除一条,然后求新图的最短路径,取这些路径中最短的一条即为第二最短路径。*同理,第n最短路径可在求解第n-1最短路径的基础上求解。 三、背包问题*部分背包问题可有贪心法求解:计算Pi/Wi数据结构:wi:第i个背包的重量;pi:第i个背包的价值; 10-1背包: 每个背包
14、只能使用一次或有限次(可转化为一次): A.求最多可放入的重量。NOIP2001 装箱问题 有一个箱子容量为v(正整数,ov20000),同时有n个物品(on30),每个物品有一个体积 (正整数)。要求从 n 个物品中,任取若千个装入箱内,使箱子的剩余空间为最小。l 搜索方法procedure search(k,v:integer); 搜索第k个物品,剩余空间为vvar i,j:integer;beginif v=best then exit; sn为前n个物品的重量和if kwk then search(k+1,v-wk);search(k+1,v);end;end; l DPFI,j为前i
15、个物品中选择若干个放入使其体积正好为j的标志,为布尔型。实现:将最优化问题转化为判定性问题f I, j = f i-1, j-wi (wI=j0 thenif j+now=n then inc(cj+now,aj);a:=c;end; 2可重复背包 A求最多可放入的重量。FI,j为前i个物品中选择若干个放入使其体积正好为j的标志,为布尔型。状态转移方程为fI,j = f I-1, j wI*k (k=1. j div wI) B.求可以放入的最大价值。USACO 1.2 Score Inflation进行一次竞赛,总时间T固定,有若干种可选择的题目,每种题目可选入的数量不限,每种题目有一个ti
16、(解答此题所需的时间)和一个si(解答此题所得的分数),现要选择若干题目,使解这些题的总时间在T以内的前提下,所得的总分最大,求最大的得分。*易想到:fi,j = max f i- k*wj, j-1 + k*pj (0=k=0 ThenBegint:=problemj.point+fi-problemj.time;If tfi Then fi:=t;End;Writeln(fM);End. C.求恰好装满的情况数。Ahoi2001 Problem2求自然数n本质不同的质数和的表达式的数目。思路一,生成每个质数的系数的排列,在一一测试,这是通法。procedure try(dep:intege
17、r);var i,j:integer;begincal; 此过程计算当前系数的计算结果,now为结果if nown then exit; 剪枝if dep=l+1 then begin 生成所有系数cal;if now=n then inc(tot);exit;end;for i:=0 to n div prdep do beginxsdep:=i;try(dep+1);xsdep:=0;end;end; 思路二,递归搜索效率较高procedure try(dep,rest:integer);var i,j,x:integer;beginif (rest0 thenfor k:=1 to n
18、div now doif j+now*k=n then inc(cj+now*k,aj);a:=c;end;mainbegin read(now); 读入第一个物品的重量i:=0; ai为背包容量为i时的放法总数while i=n do begin ai:=1; inc(i,now); end; 定义第一个物品重的整数倍的重量a值为1,作为初值for i:=2 to v dobeginread(now);update; 动态更新end;writeln(an); 四、排序算法 A.快速排序: procedure qsort(l,r:integer);var i,j,mid:integer;beg
19、ini:=l;j:=r; mid:=a(l+r) div 2; 将当前序列在中间位置的数定义为中间数repeatwhile aimid do dec(j);在右半部分寻找比中间数小的数if ij;if lj then qsort(l,j); 若未到两个数的边界,则递归搜索左右区间if ir then qsort(i,r);end;sort B.插入排序: 思路:当前a1.ai-1已排好序了,现要插入ai使a1.ai有序。procedure insert_sort;var i,j:integer;beginfor i:=2 to n do begina0:=ai;j:=i-1;while a0a
20、j then swap(ai,aj);end; D. 冒泡排序procedure bubble_sort;var i,j,k:integer;beginfor i:=1 to n-1 dofor j:=n downto i+1 doif ajaj-1 then swap( aj,aj-1); 每次比较相邻元素的关系end; E.堆排序:procedure sift(i,m:integer);调整以i为根的子树成为堆,m为结点总数var k:integer;begina0:=ai; k:=2*i;在完全二叉树中结点i的左孩子为2*i,右孩子为2*i+1while k=m do beginif (
21、km) and (akak+1) then inc(k);找出ak与ak+1中较大值if a0ak then begin ai:=ak;i:=k;k:=2*i; endelse k:=m+1;end;ai:=a0; 将根放在合适的位置end; procedure heapsort;varj:integer;beginfor j:=n div 2 downto 1 do sift(j,n);for j:=n downto 2 do beginswap(a1,aj);sift(1,j-1);end;end; F. 归并排序a为序列表,tmp为辅助数组procedure merge(var a:li
22、sttype; p,q,r:integer);将已排序好的子序列ap.q与aq+1.r合并为有序的tmpp.rvar I,j,t:integer;tmp:listtype;begint:=p;i:=p;j:=q+1;t为tmp指针,I,j分别为左右子序列的指针while (t=r) do beginif (ir) or (ai=aj) 满足取左边序列当前元素的要求then begintmpt:=ai; inc(i);endelse begintmpt:=aj;inc(j);end;inc(t);end;for i:=p to r do ai:=tmpi;end;merge procedure merge_sort(var a:listtype; p,r: intege
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1