資源簡介 一、數(shù)論算法1.求兩數(shù)的最大公約數(shù)function gcd(a,b:integer):integer;beginif b=0 then gcd:=aelse gcd:=gcd (b,a mod b);end ;2.求兩數(shù)的最小公倍數(shù)function lcm(a,b:integer):integer;beginif alcm:=a;while lcm mod b>0 do inc(lcm,a);end;3.素?cái)?shù)的求法A.小范圍內(nèi)判斷一個(gè)數(shù)是否為質(zhì)數(shù):function prime (n: integer): Boolean;var I: integer;beginfor I:=2 to trunc(sqrt(n)) doif n mod I=0 then beginprime:=false; exit;end;prime:=true;end;B.判斷l(xiāng)ongint范圍內(nèi)的數(shù)是否為素?cái)?shù)(包含求50000以內(nèi)的素?cái)?shù)表):procedure getprime;vari,j:longint;p:array[1..50000] of boolean;beginfillchar(p,sizeof(p),true);p[1]:=false;i:=2;while i<50000 do beginif p[i] thenbeginj:=i*2;while j<50000 dobegin {篩選法}p[j]:=false;inc(j,i);end;end;inc(i);end;l:=0;for i:=1 to 50000 doif p[i] then begininc(l);pr[l]:=i;end;end;{getprime}function prime(x:longint):boolean;var i:integer;beginprime:=false;for i:=1 to l doif pr[i]>=x then breakelse if x mod pr[i]=0 then exit;prime:=true;end;{prime}二、圖論算法1.最小生成樹A.Prim算法:procedure prim(v0:integer);varlowcost,closest:array[1..maxn] of integer;i,j,k,min:integer;beginfor i:=1 to n do beginlowcost:=cost[v0,i];closest:=v0;end;for i:=1 to n-1 do begin{尋找離生成樹最近的未加入頂點(diǎn)k}min:=maxlongint;for j:=1 to n doif (lowcost[j]0) then beginmin:=lowcost[j];k:=j;end;lowcost[k]:=0; {將頂點(diǎn)k加入生成樹}{生成樹中增加一條新的邊k到closest[k]}{修正各點(diǎn)的lowcost和closest值}for j:=1 to n doif cost[k,j]lowcost[j]:=cost[k,j];closest[j]:=k;end;end;end;{prim}B.Kruskal算法:(貪心)按權(quán)值遞增順序刪去圖中的邊,若不形成回路則將此邊加入最小生成樹。function find(v:integer):integer; {返回頂點(diǎn)v所在的集合}var i:integer;begini:=1;while (i<=n) and (not v in vset) do inc(i);if i<=n then find:=i else find:=0;end;procedure kruskal;vartot,i,j:integer;beginfor i:=1 to n do vset:=;{初始化定義n個(gè)集合,第I個(gè)集合包含一個(gè)元素I}p:=n-1; q:=1; tot:=0; {p為尚待加入的邊數(shù),q為邊集指針}sort;{對所有邊按權(quán)值遞增排序,存于e中,e.v1與e.v2為邊I所連接的兩個(gè)頂點(diǎn)的序號,e.len為第I條邊的長度}while p>0 do begini:=find(e[q].v1);j:=find(e[q].v2);if i<>j then begininc(tot,e[q].len);vset:=vset+vset[j];vset[j]:=[];dec(p);end;inc(q);end;writeln(tot);end;2.最短路徑A.標(biāo)號法求解單源點(diǎn)最短路徑:vara:array[1..maxn,1..maxn] of integer;b:array[1..maxn] of integer; {b指頂點(diǎn)i到源點(diǎn)的最短路徑}mark:array[1..maxn] of boolean;procedure bhf;varbest,best_j:integer;beginfillchar(mark,sizeof(mark),false);mark[1]:=true; b[1]:=0;{1為源點(diǎn)}repeatbest:=0;for i:=1 to n doIf mark then {對每一個(gè)已計(jì)算出最短路徑的點(diǎn)}for j:=1 to n doif (not mark[j]) and (a[i,j]>0) thenif (best=0) or (b+a[i,j]best:=b+a[i,j]; best_j:=j;end;if best>0 then beginb[best_j]:=best;mark[best_j]:=true;end;until best=0;end;{bhf}B.Floyed算法求解所有頂點(diǎn)對之間的最短路徑:procedure floyed;beginfor I:=1 to n dofor j:=1 to n doif a[I,j]>0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]表示I到j(luò)的最短路徑上j的前驅(qū)結(jié)點(diǎn)}for k:=1 to n do {枚舉中間結(jié)點(diǎn)}for i:=1 to n dofor j:=1 to n doif a[i,k]+a[j,k]a[i,j]:=a[i,k]+a[k,j];p[I,j]:=p[k,j];end;end;C. Dijkstra 算法:vara:array[1..maxn,1..maxn] of integer;b,pre:array[1..maxn] of integer; {pre指最短路徑上I的前驅(qū)結(jié)點(diǎn)}mark:array[1..maxn] of boolean;procedure dijkstra(v0:integer);beginfillchar(mark,sizeof(mark),false);for i:=1 to n do begind:=a[v0,i];if d<>0 then pre:=v0 else pre:=0;end;mark[v0]:=true;repeat {每循環(huán)一次加入一個(gè)離1集合最近的結(jié)點(diǎn)并調(diào)整其他結(jié)點(diǎn)的參數(shù)}min:=maxint; u:=0; {u記錄離1集合最近的結(jié)點(diǎn)}for i:=1 to n doif (not mark) and (du:=i; min:=d;end;if u<>0 then beginmark[u]:=true;for i:=1 to n doif (not mark) and (a[u,i]+d[u]d:=a[u,i]+d[u];pre:=u;end;end;until u=0;end;3.計(jì)算圖的傳遞閉包Procedure Longlink;VarT:array[1..maxn,1..maxn] of boolean;BeginFillchar(t,sizeof(t),false);For k:=1 to n doFor I:=1 to n doFor j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);End;4.無向圖的連通分量A.深度優(yōu)先procedure dfs ( now,color: integer);beginfor i:=1 to n doif a[now,i] and c=0 then begin {對結(jié)點(diǎn)I染色}c:=color;dfs(I,color);end;end;B 寬度優(yōu)先(種子染色法)5.關(guān)鍵路徑幾個(gè)定義: 頂點(diǎn)1為源點(diǎn),n為匯點(diǎn)。a. 頂點(diǎn)事件最早發(fā)生時(shí)間Ve[j], Ve [j] = max{ Ve [j] + w[I,j] },其中Ve (1) = 0;b. 頂點(diǎn)事件最晚發(fā)生時(shí)間 Vl[j], Vl [j] = min{ Vl[j] – w[I,j] },其中 Vl(n) = Ve(n);c. 邊活動(dòng)最早開始時(shí)間 Ee, 若邊I由表示,則Ee = Ve[j];d. 邊活動(dòng)最晚開始時(shí)間 El, 若邊I由表示,則El = Vl[k] – w[j,k];若 Ee[j] = El[j] ,則活動(dòng)j為關(guān)鍵活動(dòng),由關(guān)鍵活動(dòng)組成的路徑為關(guān)鍵路徑。求解方法:a. 從源點(diǎn)起topsort,判斷是否有回路并計(jì)算Ve;b. 從匯點(diǎn)起topsort,求Vl;c. 算Ee 和 El;6.拓?fù)渑判?br/>找入度為0的點(diǎn),刪去與其相連的所有邊,不斷重復(fù)這一過程。例 尋找一數(shù)列,其中任意連續(xù)p項(xiàng)之和為正,任意q 項(xiàng)之和為負(fù),若不存在則輸出NO. 展開更多...... 收起↑ 資源預(yù)覽 縮略圖、資源來源于二一教育資源庫