国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C C++ 算法實例大全

2020-01-26 14:22:38
字體:
來源:轉載
供稿:網友

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 a<b then swap(a,b);lcm:=a;while lcm mod b>0 do inc(lcm,a);end; 

3.素數的求法
A.小范圍內判斷一個數是否為質數:

function prime (n: integer): Boolean;var I: integer;beginfor I:=2 to trunc(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:array[1..50000] of boolean;beginfillchar(p,sizeof(p),true);p[1]:=false;i:=2;while i<50000 do beginif p[i] then beginj:=i*2;while j<50000 do beginp[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):integer;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[i]:=cost[v0,i];closest[i]:=v0;end;for i:=1 to n-1 do begin{尋找離生成樹最近的未加入頂點k}min:=maxlongint;for j:=1 to n doif (lowcost[j]<min) and (lowcost[j]<>0) then beginmin:=lowcost[j];k:=j;end;lowcost[k]:=0; {將頂點k加入生成樹}{生成樹中增加一條新的邊k到closest[k]}{修正各點的lowcost和closest值}for j:=1 to n doif cost[k,j]<lwocost[j] then beginlowcost[j]:=cost[k,j];closest[j]:=k;end;end;end;{prim}

B.Kruskal算法:(貪心)

按權值遞增順序刪去圖中的邊,若不形成回路則將此邊加入最小生成樹。

function find(v:integer):integer; {返回頂點v所在的集合}var i:integer;begini:=1;while (i<=n) and (not v in vset[i]) 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[i]:=[i];{初始化定義n個集合,第I個集合包含一個元素I}p:=n-1; q:=1; tot:=0; {p為尚待加入的邊數,q為邊集指針}sort;{對所有邊按權值遞增排序,存于e[I]中,e[I].v1與e[I].v2為邊I所連接的兩個頂點的序號,e[I].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[i]:=vset[i]+vset[j];vset[j]:=[];dec(p);end;inc(q);end;writeln(tot);end;

2.最短路徑

A.標號法求解單源點最短路徑:

vara:array[1..maxn,1..maxn] of integer;b:array[1..maxn] of integer; {b[i]指頂點i到源點的最短路徑}mark:array[1..maxn] of boolean; procedure bhf;varbest,best_j:integer;beginfillchar(mark,sizeof(mark),false);mark[1]:=true; b[1]:=0;{1為源點}repeatbest:=0;for i:=1 to n doIf mark[i] then {對每一個已計算出最短路徑的點}for j:=1 to n doif (not mark[j]) and (a[i,j]>0) then if (best=0) or (b[i]+a[i,j]<best) then beginbest:=b[i]+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算法求解所有頂點對之間的最短路徑:

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的最短路徑上j的前驅結點}for k:=1 to n do {枚舉中間結點}for i:=1 to n dofor j:=1 to n doif a[i,k]+a[j,k]<a[i,j] then begina[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]指最短路徑上I的前驅結點}mark:array[1..maxn] of boolean;procedure dijkstra(v0:integer);beginfillchar(mark,sizeof(mark),false);for i:=1 to n do begind[i]:=a[v0,i];if d[i]<>0 then pre[i]:=v0 else pre[i]:=0;end;mark[v0]:=true;repeat {每循環一次加入一個離1集合最近的結點并調整其他結點的參數}min:=maxint; u:=0; {u記錄離1集合最近的結點}for i:=1 to n doif (not mark[i]) and (d[i]<min) then beginu:=i; min:=d[i];end;if u<>0 then beginmark[u]:=true; for i:=1 to n doif (not mark[i]) and (a[u,i]+d[u]<d[i]) then begind[i]:=a[u,i]+d[u];pre[i]:=u;end;end;until u=0;end;

3.計算圖的傳遞閉包

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.深度優先

procedure dfs ( now,color: integer);beginfor i:=1 to n doif a[now,i] and c[i]=0 then begin {對結點I染色}c[i]:=color;dfs(I,color);end;end; 

B 寬度優先(種子染色法)


5.關鍵路徑

幾個定義: 頂點1為源點,n為匯點。
a. 頂點事件最早發生時間Ve[j], Ve [j] = max{ Ve [j] + w[I,j] },其中Ve (1) = 0;
b. 頂點事件最晚發生時間 Vl[j], Vl [j] = min{ Vl[j]

主站蜘蛛池模板: 霍州市| 罗源县| 嘉义市| 丽水市| 电白县| 十堰市| 鄂尔多斯市| 临清市| 曲周县| 临湘市| 莱芜市| 澄江县| 碌曲县| 拜城县| 平定县| 凤山市| 永平县| 正镶白旗| 肃宁县| 德惠市| 迁西县| 南郑县| 古田县| 中卫市| 邵阳市| 正镶白旗| 保靖县| 文山县| 墨玉县| 灯塔市| 全椒县| 滨州市| 星子县| 阆中市| 潞西市| 昌邑市| 东城区| 宜昌市| 丰台区| 宁化县| 绵阳市|