C C++ 算法實(shí)例大全
C C++,算法實(shí)例
一、數(shù)論算法
1.求兩數(shù)的最大公約數(shù)
function gcd(a,b:integer):integer; begin if b=0 then gcd:=a else gcd:=gcd (b,a mod b); end ;
2.求兩數(shù)的最小公倍數(shù)
function lcm(a,b:integer):integer; begin if a<b then swap(a,b); lcm:=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; begin for I:=2 to trunc(sqrt(n)) do if n mod I=0 then begin prime:=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;
var
i,j:longint;
p:array[1..50000] of boolean;
begin
fillchar(p,sizeof(p),true);
p[1]:=false;
i:=2;
while i<50000 do begin
if p[i] then begin
j:=i*2;
while j<50000 do begin
p[j]:=false;
inc(j,i);
end;
end;
inc(i);
end;
l:=0;
for i:=1 to 50000 do
if p[i] then begin
inc(l);pr[l]:=i;
end;
end;{getprime}
function prime(x:longint):integer;
var i:integer;
begin
prime:=false;
for i:=1 to l do
if pr[i]>=x then break
else if x mod pr[i]=0 then exit;
prime:=true;
end;{prime}
二、圖論算法
1.最小生成樹
A.Prim算法:
procedure prim(v0:integer);
var
lowcost,closest:array[1..maxn] of integer;
i,j,k,min:integer;
begin
for i:=1 to n do begin
lowcost[i]:=cost[v0,i];
closest[i]:=v0;
end;
for i:=1 to n-1 do begin
{尋找離生成樹最近的未加入頂點(diǎn)k}
min:=maxlongint;
for j:=1 to n do
if (lowcost[j]<min) and (lowcost[j]<>0) then begin
min:=lowcost[j];
k:=j;
end;
lowcost[k]:=0; {將頂點(diǎn)k加入生成樹}
{生成樹中增加一條新的邊k到closest[k]}
{修正各點(diǎn)的lowcost和closest值}
for j:=1 to n do
if cost[k,j]<lwocost[j] then begin
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;
begin
i:=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;
var
tot,i,j:integer;
begin
for i:=1 to n do vset[i]:=[i];{初始化定義n個(gè)集合,第I個(gè)集合包含一個(gè)元素I}
p:=n-1; q:=1; tot:=0; {p為尚待加入的邊數(shù),q為邊集指針}
sort;
{對(duì)所有邊按權(quán)值遞增排序,存于e[I]中,e[I].v1與e[I].v2為邊I所連接的兩個(gè)頂點(diǎn)的序號(hào),e[I].len為第I條邊的長度}
while p>0 do begin
i:=find(e[q].v1);j:=find(e[q].v2);
if i<>j then begin
inc(tot,e[q].len);
vset[i]:=vset[i]+vset[j];vset[j]:=[];
dec(p);
end;
inc(q);
end;
writeln(tot);
end;
2.最短路徑
A.標(biāo)號(hào)法求解單源點(diǎn)最短路徑:
var
a:array[1..maxn,1..maxn] of integer;
b:array[1..maxn] of integer; {b[i]指頂點(diǎn)i到源點(diǎn)的最短路徑}
mark:array[1..maxn] of boolean;
procedure bhf;
var
best,best_j:integer;
begin
fillchar(mark,sizeof(mark),false);
mark[1]:=true; b[1]:=0;{1為源點(diǎn)}
repeat
best:=0;
for i:=1 to n do
If mark[i] then {對(duì)每一個(gè)已計(jì)算出最短路徑的點(diǎn)}
for j:=1 to n do
if (not mark[j]) and (a[i,j]>0) then
if (best=0) or (b[i]+a[i,j]<best) then begin
best:=b[i]+a[i,j]; best_j:=j;
end;
if best>0 then begin
b[best_j]:=best;mark[best_j]:=true;
end;
until best=0;
end;{bhf}
B.Floyed算法求解所有頂點(diǎn)對(duì)之間的最短路徑:
procedure floyed;
begin
for I:=1 to n do
for j:=1 to n do
if 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 do
for j:=1 to n do
if a[i,k]+a[j,k]<a[i,j] then begin
a[i,j]:=a[i,k]+a[k,j];
p[I,j]:=p[k,j];
end;
end;
C. Dijkstra 算法:
var
a:array[1..maxn,1..maxn] of integer;
b,pre:array[1..maxn] of integer; {pre[i]指最短路徑上I的前驅(qū)結(jié)點(diǎn)}
mark:array[1..maxn] of boolean;
procedure dijkstra(v0:integer);
begin
fillchar(mark,sizeof(mark),false);
for i:=1 to n do begin
d[i]:=a[v0,i];
if d[i]<>0 then pre[i]:=v0 else pre[i]:=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 do
if (not mark[i]) and (d[i]<min) then begin
u:=i; min:=d[i];
end;
if u<>0 then begin
mark[u]:=true;
for i:=1 to n do
if (not mark[i]) and (a[u,i]+d[u]<d[i]) then begin
d[i]:=a[u,i]+d[u];
pre[i]:=u;
end;
end;
until u=0;
end;
3.計(jì)算圖的傳遞閉包
Procedure Longlink; Var T:array[1..maxn,1..maxn] of boolean; Begin Fillchar(t,sizeof(t),false); For k:=1 to n do For I:=1 to n do For 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);
begin
for i:=1 to n do
if a[now,i] and c[i]=0 then begin {對(duì)結(jié)點(diǎn)I染色}
c[i]:=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], 若邊I由<j,k>表示,則Ee[I] = Ve[j];
d. 邊活動(dòng)最晚開始時(shí)間 El[I], 若邊I由<j,k>表示,則El[I] = 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ù)渑判?/p>
找入度為0的點(diǎn),刪去與其相連的所有邊,不斷重復(fù)這一過程。
例 尋找一數(shù)列,其中任意連續(xù)p項(xiàng)之和為正,任意q 項(xiàng)之和為負(fù),若不存在則輸出NO.
7.回路問題
Euler回路(DFS)
定義:經(jīng)過圖的每條邊僅一次的回路。(充要條件:圖連同且無奇點(diǎn))
Hamilton回路
定義:經(jīng)過圖的每個(gè)頂點(diǎn)僅一次的回路。
一筆畫
充要條件:圖連通且奇點(diǎn)個(gè)數(shù)為0個(gè)或2個(gè)。
9.判斷圖中是否有負(fù)權(quán)回路 Bellman-ford 算法
x[I],y[I],t[I]分別表示第I條邊的起點(diǎn),終點(diǎn)和權(quán)。共n個(gè)結(jié)點(diǎn)和m條邊。
procedure bellman-ford
begin
for I:=0 to n-1 do d[I]:=+infinitive;
d[0]:=0;
for I:=1 to n-1 do
for j:=1 to m do {枚舉每一條邊}
if d[x[j]]+t[j]<d[y[j]] then d[y[j]]:=d[x[j]]+t[j];
for I:=1 to m do
if d[x[j]]+t[j]<d[y[j]] then return false else return true;
end;
10.第n最短路徑問題
*第二最短路徑:每舉最短路徑上的每條邊,每次刪除一條,然后求新圖的最短路徑,取這些路徑中最短的一條即為第二最短路徑。
*同理,第n最短路徑可在求解第n-1最短路徑的基礎(chǔ)上求解。
三、背包問題
*部分背包問題可有貪心法求解:計(jì)算Pi/Wi
數(shù)據(jù)結(jié)構(gòu):
w[i]:第i個(gè)背包的重量;
p[i]:第i個(gè)背包的價(jià)值;
1.0-1背包: 每個(gè)背包只能使用一次或有限次(可轉(zhuǎn)化為一次):
A.求最多可放入的重量。
NOIP2001 裝箱問題
有一個(gè)箱子容量為v(正整數(shù),o≤v≤20000),同時(shí)有n個(gè)物品(o≤n≤30),每個(gè)物品有一個(gè)體積 (正整數(shù))。要求從 n 個(gè)物品中,任取若千個(gè)裝入箱內(nèi),使箱子的剩余空間為最小。
l 搜索方法
procedure search(k,v:integer); {搜索第k個(gè)物品,剩余空間為v}
var i,j:integer;
begin
if v<best then best:=v;
if v-(s[n]-s[k-1])>=best then exit; {s[n]為前n個(gè)物品的重量和}
if k<=n then begin
if v>w[k] then search(k+1,v-w[k]);
search(k+1,v);
end;
end;
l DP
F[I,j]為前i個(gè)物品中選擇若干個(gè)放入使其體積正好為j的標(biāo)志,為布爾型。
實(shí)現(xiàn):將最優(yōu)化問題轉(zhuǎn)化為判定性問題
f [I, j] = f [ i-1, j-w[i] ] (w[I]<=j<=v) 邊界:f[0,0]:=true.
For I:=1 to n do
For j:=w[I] to v do F[I,j]:=f[I-1,j-w[I]];
優(yōu)化:當(dāng)前狀態(tài)只與前一階段狀態(tài)有關(guān),可降至一維。
F[0]:=true;
For I:=1 to n do begin
F1:=f;
For j:=w[I] to v do
If f[j-w[I]] then f1[j]:=true;
F:=f1;
End;
B.求可以放入的最大價(jià)值。
F[I,j] 為容量為I時(shí)取前j個(gè)背包所能獲得的最大價(jià)值。
F [i,j] = max { f [ i – w [ j ], j-1] + p [ j ], f[ i,j-1] }
C.求恰好裝滿的情況數(shù)。
DP:
Procedure update; var j,k:integer; begin c:=a; for j:=0 to n do if a[j]>0 then if j+now<=n then inc(c[j+now],a[j]); a:=c; end;
2.可重復(fù)背包
A求最多可放入的重量。
F[I,j]為前i個(gè)物品中選擇若干個(gè)放入使其體積正好為j的標(biāo)志,為布爾型。
狀態(tài)轉(zhuǎn)移方程為
f[I,j] = f [ I-1, j – w[I]*k ] (k=1.. j div w[I])
B.求可以放入的最大價(jià)值。
USACO 1.2 Score Inflation
進(jìn)行一次競賽,總時(shí)間T固定,有若干種可選擇的題目,每種題目可選入的數(shù)量不限,每種題目有一個(gè)ti(解答此題所需的時(shí)間)和一個(gè)si(解答此題所得的分?jǐn)?shù)),現(xiàn)要選擇若干題目,使解這些題的總時(shí)間在T以內(nèi)的前提下,所得的總分最大,求最大的得分。
*易想到:
f[i,j] = max { f [i- k*w[j], j-1] + k*p[j] } (0<=k<= i div w[j])
其中f[i,j]表示容量為i時(shí)取前j種背包所能達(dá)到的最大值。
*實(shí)現(xiàn):
Begin FillChar(f,SizeOf(f),0); For i:=1 To M Do For j:=1 To N Do If i-problem[j].time>=0 Then Begin t:=problem[j].point+f[i-problem[j].time]; If t>f[i] Then f[i]:=t; End; Writeln(f[M]); End.
C.求恰好裝滿的情況數(shù)。
Ahoi2001 Problem2
求自然數(shù)n本質(zhì)不同的質(zhì)數(shù)和的表達(dá)式的數(shù)目。
思路一,生成每個(gè)質(zhì)數(shù)的系數(shù)的排列,在一一測試,這是通法。
procedure try(dep:integer);
var i,j:integer;
begin
cal; {此過程計(jì)算當(dāng)前系數(shù)的計(jì)算結(jié)果,now為結(jié)果}
if now>n then exit; {剪枝}
if dep=l+1 then begin {生成所有系數(shù)}
cal;
if now=n then inc(tot);
exit;
end;
for i:=0 to n div pr[dep] do begin
xs[dep]:=i;
try(dep+1);
xs[dep]:=0;
end;
end;
思路二,遞歸搜索效率較高
procedure try(dep,rest:integer);
var i,j,x:integer;
begin
if (rest<=0) or (dep=l+1) then begin
if rest=0 then inc(tot);
exit;
end;
for i:=0 to rest div pr[dep] do
try(dep+1,rest-pr[dep]*i);
end;
{main: try(1,n); }
思路三:可使用動(dòng)態(tài)規(guī)劃求解
USACO1.2 money system
V個(gè)物品,背包容量為n,求放法總數(shù)。
轉(zhuǎn)移方程:
Procedure update;
var j,k:integer;
begin
c:=a;
for j:=0 to n do
if a[j]>0 then
for k:=1 to n div now do
if j+now*k<=n then inc(c[j+now*k],a[j]);
a:=c;
end;
{main}
begin
read(now); {讀入第一個(gè)物品的重量}
i:=0; {a[i]為背包容量為i時(shí)的放法總數(shù)}
while i<=n do begin
a[i]:=1; inc(i,now); end; {定義第一個(gè)物品重的整數(shù)倍的重量a值為1,作為初值}
for i:=2 to v do
begin
read(now);
update; {動(dòng)態(tài)更新}
end;
writeln(a[n]);
四、排序算法
A.快速排序:
procedure qsort(l,r:integer);
var i,j,mid:integer;
begin
i:=l;j:=r; mid:=a[(l+r) div 2]; {將當(dāng)前序列在中間位置的數(shù)定義為中間數(shù)}
repeat
while a[i]<mid do inc(i); {在左半部分尋找比中間數(shù)大的數(shù)}
while a[j]>mid do dec(j);{在右半部分尋找比中間數(shù)小的數(shù)}
if i<=j then begin {若找到一組與排序目標(biāo)不一致的數(shù)對(duì)則交換它們}
swap(a[i],a[j]);
inc(i);dec(j); {繼續(xù)找}
end;
until i>j;
if l<j then qsort(l,j); {若未到兩個(gè)數(shù)的邊界,則遞歸搜索左右區(qū)間}
if i<r then qsort(i,r);
end;{sort}
B.插入排序:
思路:當(dāng)前a[1]..a[i-1]已排好序了,現(xiàn)要插入a[i]使a[1]..a[i]有序。
procedure insert_sort;
var i,j:integer;
begin
for i:=2 to n do begin
a[0]:=a[i];
j:=i-1;
while a[0]<a[j] do begin
a[j+1]:=a[j];
j:=j-1;
end;
a[j+1]:=a[0];
end;
end;{inset_sort}
C.選擇排序:
procedure sort; var i,j,k:integer; begin for i:=1 to n-1 do for j:=i+1 to n do if a[i]>a[j] then swap(a[i],a[j]); end;
D. 冒泡排序
procedure bubble_sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=n downto i+1 do
if a[j]<a[j-1] then swap( a[j],a[j-1]); {每次比較相鄰元素的關(guān)系}
end;
E.堆排序:
procedure sift(i,m:integer);{調(diào)整以i為根的子樹成為堆,m為結(jié)點(diǎn)總數(shù)}
var k:integer;
begin
a[0]:=a[i]; k:=2*i;{在完全二叉樹中結(jié)點(diǎn)i的左孩子為2*i,右孩子為2*i+1}
while k<=m do begin
if (k<m) and (a[k]<a[k+1]) then inc(k);{找出a[k]與a[k+1]中較大值}
if a[0]<a[k] then begin a[i]:=a[k];i:=k;k:=2*i; end
else k:=m+1;
end;
a[i]:=a[0]; {將根放在合適的位置}
end;
procedure heapsort;
var
j:integer;
begin
for j:=n div 2 downto 1 do sift(j,n);
for j:=n downto 2 do begin
swap(a[1],a[j]);
sift(1,j-1);
end;
end;
F. 歸并排序
{a為序列表,tmp為輔助數(shù)組}
procedure merge(var a:listtype; p,q,r:integer);
{將已排序好的子序列a[p..q]與a[q+1..r]合并為有序的tmp[p..r]}
var I,j,t:integer;
tmp:listtype;
begin
t:=p;i:=p;j:=q+1;{t為tmp指針,I,j分別為左右子序列的指針}
while (t<=r) do begin
if (i<=q){左序列有剩余} and ((j>r) or (a[i]<=a[j])) {滿足取左邊序列當(dāng)前元素的要求}
then begin
tmp[t]:=a[i]; inc(i);
end
else begin
tmp[t]:=a[j];inc(j);
end;
inc(t);
end;
for i:=p to r do a[i]:=tmp[i];
end;{merge}
procedure merge_sort(var a:listtype; p,r: integer); {合并排序a[p..r]}
var q:integer;
begin
if p<>r then begin
q:=(p+r-1) div 2;
merge_sort (a,p,q);
merge_sort (a,q+1,r);
merge (a,p,q,r);
end;
end;
{main}
begin
merge_sort(a,1,n);
end.
G.基數(shù)排序
思想:對(duì)每個(gè)元素按從低位到高位對(duì)每一位進(jìn)行一次排序
五、高精度計(jì)算
高精度數(shù)的定義:
type
hp=array[1..maxlen] of integer;
1.高精度加法
procedure plus ( a,b:hp; var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a[0]>b[0] then len:=a[0] else len:=b[0];
for i:=1 to len do begin
inc(c[i],a[i]+b[i]);
if c[i]>10 then begin dec(c[i],10); inc(c[i+1]); end; {進(jìn)位}
end;
if c[len+1]>0 then inc(len);
c[0]:=len;
end;{plus}
2.高精度減法
procedure substract(a,b:hp;var c:hp); var i,len:integer; begin fillchar(c,sizeof(c),0); if a[0]>b[0] then len:=a[0] else len:=b[0]; for i:=1 to len do begin inc(c[i],a[i]-b[i]); if c[i]<0 then begin inc(c[i],10);dec(c[i+1]); end; while (len>1) and (c[len]=0) do dec(len); c[0]:=len; end;
3.高精度乘以低精度
procedure multiply(a:hp;b:longint;var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a[0];
for i:=1 to len do begin
inc(c[i],a[i]*b);
inc(c[i+1],(a[i]*b) div 10);
c[i]:=c[i] mod 10;
end;
inc(len);
while (c[len]>=10) do begin {處理最高位的進(jìn)位}
c[len+1]:=c[len] div 10;
c[len]:=c[len] mod 10;
inc(len);
end;
while (len>1) and (c[len]=0) do dec(len); {若不需進(jìn)位則調(diào)整len}
c[0]:=len;
end;{multiply}
4.高精度乘以高精度
procedure high_multiply(a,b:hp; var c:hp} var i,j,len:integer; begin fillchar(c,sizeof(c),0); for i:=1 to a[0] do for j:=1 to b[0] do begin inc(c[i+j-1],a[i]*b[j]); inc(c[i+j],c[i+j-1] div 10); c[i+j-1]:=c[i+j-1] mod 10; end; len:=a[0]+b[0]+1; while (len>1) and (c[len]=0) do dec(len); c[0]:=len; end;
5.高精度除以低精度
procedure devide(a:hp;b:longint; var c:hp; var d:longint);
{c:=a div b; d:= a mod b}
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a[0]; d:=0;
for i:=len downto 1 do begin
d:=d*10+a[i];
c[i]:=d div b;
d:=d mod b;
end;
while (len>1) and (c[len]=0) then dec(len);
c[0]:=len;
end;
6.高精度除以高精度
procedure high_devide(a,b:hp; var c,d:hp);
var
i,len:integer;
begin
fillchar(c,sizeof(c),0);
fillchar(d,sizeof(d),0);
len:=a[0];d[0]:=1;
for i:=len downto 1 do begin
multiply(d,10,d);
d[1]:=a[i];
while(compare(d,b)>=0) do {即d>=b}
begin
Subtract(d,b,d);
inc(c[i]);
end;
end;
while(len>1)and(c.s[len]=0) do dec(len);
c.len:=len;
end;
六、 樹的遍歷
1.已知前序中序求后序
procedure Solve(pre,mid:string);
var i:integer;
begin
if (pre='''') or (mid='''') then exit;
i:=pos(pre[1],mid);
solve(copy(pre,2,i),copy(mid,1,i-1));
solve(copy(pre,i+1,length(pre)-i),copy(mid,i+1,length(mid)-i));
post:=post+pre[1]; {加上根,遞歸結(jié)束后post即為后序遍歷}
end;
2.已知中序后序求前序
procedure Solve(mid,post:string);
var i:integer;
begin
if (mid='''') or (post='''') then exit;
i:=pos(post[length(post)],mid);
pre:=pre+post[length(post)]; {加上根,遞歸結(jié)束后pre即為前序遍歷}
solve(copy(mid,1,I-1),copy(post,1,I-1));
solve(copy(mid,I+1,length(mid)-I),copy(post,I,length(post)-i));
end;
3.已知前序后序求中序的一種
function ok(s1,s2:string):boolean; var i,l:integer; p:boolean; begin ok:=true; l:=length(s1); for i:=1 to l do begin p:=false; for j:=1 to l do if s1[i]=s2[j] then p:=true; if not p then begin ok:=false;exit;end; end; end; procedure solve(pre,post:string); var i:integer; begin if (pre='''') or (post='''') then exit; i:=0; repeat inc(i); until ok(copy(pre,2,i),copy(post,1,i)); solve(copy(pre,2,i),copy(post,1,i)); midstr:=midstr+pre[1]; solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1)); end;
七 進(jìn)制轉(zhuǎn)換
1.任意正整數(shù)進(jìn)制間的互化
除n取余
2.實(shí)數(shù)任意正整數(shù)進(jìn)制間的互化
乘n取整
3.負(fù)數(shù)進(jìn)制:
設(shè)計(jì)一個(gè)程序,讀入一個(gè)十進(jìn)制數(shù)的基數(shù)和一個(gè)負(fù)進(jìn)制數(shù)的基數(shù),并將此十進(jìn)制數(shù)轉(zhuǎn)換為此負(fù) 進(jìn)制下的數(shù):-R∈{-2,-3,-4,....-20}
八 全排列與組合的生成
1.排列的生成:(1..n)
procedure solve(dep:integer);
var
i:integer;
begin
if dep=n+1 then begin writeln(s);exit; end;
for i:=1 to n do
if not used[i] then begin
s:=s+chr(i+ord(''0''));used[i]:=true;
solve(dep+1);
s:=copy(s,1,length(s)-1); used[i]:=false;
end;
end;
2.組合的生成(1..n中選取k個(gè)數(shù)的所有方案)
procedure solve(dep,pre:integer);
var
i:integer;
begin
if dep=k+1 then begin writeln(s);exit; end;
for i:=1 to n do
if (not used[i]) and (i>pre) then begin
s:=s+chr(i+ord(''0''));used[i]:=true;
solve(dep+1,i);
s:=copy(s,1,length(s)-1); used[i]:=false;
end;
end;
九.查找算法
1.折半查找
function binsearch(k:keytype):integer; var low,hig,mid:integer; begin low:=1;hig:=n; mid:=(low+hig) div 2; while (a[mid].key<>k) and (low<=hig) do begin if a[mid].key>k then hig:=mid-1 else low:=mid+1; mid:=(low+hig) div 2; end; if low>hig then mid:=0; binsearch:=mid; end;
2.樹形查找
二叉排序樹:每個(gè)結(jié)點(diǎn)的值都大于其左子樹任一結(jié)點(diǎn)的值而小于其右子樹任一結(jié)點(diǎn)的值。
查找
function treesrh(k:keytype):pointer; var q:pointer; begin q:=root; while (q<>nil) and (q^.key<>k) do if k<q^.key then q:=q^.left else q:=q^.right; treesrh:=q; end;
十、貪心
*會(huì)議問題
(1) n個(gè)活動(dòng)每個(gè)活動(dòng)有一個(gè)開始時(shí)間和一個(gè)結(jié)束時(shí)間,任一時(shí)刻僅一項(xiàng)活動(dòng)進(jìn)行,求滿足活動(dòng)數(shù)最多的情況。
解:按每項(xiàng)活動(dòng)的結(jié)束時(shí)間進(jìn)行排序,排在前面的優(yōu)先滿足。
(2)會(huì)議室空閑時(shí)間最少。
(3)每個(gè)客戶有一個(gè)愿付的租金,求最大利潤。
(4)共R間會(huì)議室,第i個(gè)客戶需使用i間會(huì)議室,費(fèi)用相同,求最大利潤。
十一、回溯法框架
1. n皇后問題
procedure try(i:byte); var j:byte; begin if i=n+1 then begin print;exit;end; for j:=1 to n do if a[i] and b[j+i] and c[j-i] then begin x[i]:=j; a[j]:=false; b[j+i]:=false; c[j-i]:=false; try(i+1); a[j]:=true; b[i+j]:=true; c[j-i]:=true; end; end;
2.Hanoi Tower 漢諾塔
h(n)=2*h(n-1)+1
h(1)=1
初始所有銅片都在a柱上
procedure hanoi(n,a,b,c:byte); {將第n塊銅片從a柱通過b柱移到c柱上}
begin
if n=0 then exit;
hanoi(n-1,a,c,b); {將上面的n-1塊從a柱通過c柱移到b柱上}
write(n,'moved from',a,'to',c);
hanoi(n-1,b,a,c);{ 將b上的n-1塊從b柱通過a柱移到c柱上
end;
初始銅片分布在3個(gè)柱上,給定目標(biāo)柱goal
h[1..3,0..n]存放三個(gè)柱的狀態(tài),now與nowp存最大的不到位的銅片的柱號(hào)和編號(hào),h[I,0]存第I個(gè)柱上的個(gè)數(shù)。
Procedure move(k,goal:integer); {將最大不到位的k移到目標(biāo)柱goal上}
Begin
If k=0 then exit;
For I:=1 to 3 do
For j:=1 to han[I,0] do
If h[I,j]=k then begin now:=I;nowp:=j; end; {找到k的位置}
If now<>goal then begin {若未移到目標(biāo)}
Move(k-1,6-now-goal); {剩下的先移到?jīng)]用的柱上}
Writeln(k moved from now to goal);
H[goal,h[goal,0]+1]:=h[now,nowp]; h[now,nowp]:=0;
Inc(h[goal,0]); dec(h[now,0]);
Move(k-1,goal); {剩下的移到目標(biāo)上}
End;
十二、DFS框架
NOIP2001 數(shù)的劃分
procedure work(dep,pre,s:longint); {入口為work(1,1,n)}
{dep為當(dāng)前試放的第dep個(gè)數(shù),pre為前一次試放的數(shù),s為當(dāng)前剩余可分的總數(shù)}
var j:longint;
begin
if dep=n then begin
if s>=pre then inc(r); exit;
end;
for j:=pre to s div 2 do work(dep+1,j,s-j);
end;
類似:
procedure try(dep:integer);
var i:integer;
begin
if dep=k then begin
if tot>=a[dep-1] then inc(sum);
exit; end;
for i:=a[dep-1] to tot div 2 do begin
a[dep]:=i; dec(tot,i);
try(dep+1);
inc(tot,i);
end;
end;{try}
十三、BFS框架
IOI94 房間問題
head:=1; tail:=0;
while tail<head do begin
inc(tail);
for k:=1 to n do
if k方向可擴(kuò)展 then begin
inc(head);
list[head].x:=list[tail].x+dx[k]; {擴(kuò)展出新結(jié)點(diǎn)list[head]}
list[head].y:=list[tail].y+dy[k];
處理新結(jié)點(diǎn)list[head];
end;
end;
十五、數(shù)據(jù)結(jié)構(gòu)相關(guān)算法
1.鏈表的定位函數(shù)
loc(I:integer):pointer; {尋找鏈表中的第I個(gè)結(jié)點(diǎn)的指針}
procedure loc(L:linklist; I:integer):pointer;
var p:pointer;
j:integer;
begin
p:=L.head; j:=0;
if (I>=1) and (I<=L.len) then
while j<I do begin p:=p^.next; inc(j); end;
loc:=p;
end;
2.單鏈表的插入操作
procedure insert(L:linklist; I:integer; x:datatype); var p,q:pointer; begin p:=loc(L,I); new(q); q^.data:=x; q^.next:=p^.next; p^.next:=q; inc(L.len); end;
3.單鏈表的刪除操作
procedure delete(L:linklist; I:integer); var p,q:pointer; begin p:=loc(L,I-1); q:=p^.next; p^.next:=q^.next; dispose(q); dec(L.len); end;
4.雙鏈表的插入操作(插入新結(jié)點(diǎn)q)
p:=loc(L,I); new(q); q^.data:=x; q^.pre:=p; q^.next:=p^.next; p^.next:=q; q^.next^.pre:=q;
5.雙鏈表的刪除操作
p:=loc(L,I); {p為要?jiǎng)h除的結(jié)點(diǎn)}
p^.pre^.next:=p^.next;
p^.next^.pre:=p^.pre;
dispose(p);
上一篇:C++跳轉(zhuǎn)語句之Goto對(duì)變量定義的影響詳解
欄 目:C語言
下一篇:c++獲取sqlite3數(shù)據(jù)庫表中所有字段的方法小結(jié)
本文標(biāo)題:C C++ 算法實(shí)例大全
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1960.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入第K大數(shù)問題以及算法概要的詳解
- 01-10c++中inline的用法分析
- 01-10深入N皇后問題的兩個(gè)最高效算法的詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法


