打不死的心态活到老。

基本算法(用 PASCAL 描述)

上一篇 / 下一篇  2007-11-16 12:14:31 / 个人分类:Delphi编程

(`;qF6U2_2`~c(R0转:http://www.cnblogs.com/dingjie/archive/2005/07/15/193216.html51Testing软件测试网2jk9V#pj-H&D4~

JN6Oq9X#?S&`ID0基本算法(用 PASCAL 描述)51Testing软件测试网Hw!Q@!W2L!z

1.数论算法
N[ b?0BVYkL)Z0求两数的最大公约数51Testing软件测试网w MippHH_n
function gcd(a,b:integer):integer;
r3dDI}5a8xDSGb0begin
8PU:ip5~7^Q5_cu1F0if b=0 then gcd:=a51Testing软件测试网M bF1|p B/o
else gcd:=gcd (b,a mod B);51Testing软件测试网.eN l2Vs D0F&{8e?
end;
{R*G,pk4gn0
C"[.k o/g$zB%n5n0求两数的最小公倍数
{.u ?f@+XT4{0function lcm(a,b:integer):integer;51Testing软件测试网#NlwHU|4]
begin
9xo/};X4t@^0if a< b then swap(a,B);51Testing软件测试网$K q;K'~@!H L+lC/Y
lcm:=a;
;?`]1d rzie'D$p0while lcm mod b >0 do inc(lcm,a);51Testing软件测试网:~7p6x'\`c I'L
end;
I9r(yxf[F^051Testing软件测试网jE#C#\6n3k
素数的求法
.N_v _ @$D$u;T0A.小范围内判断一个数是否为质数:
`7[;u\t%G2Z0function prime (n: integer): Boolean;
l.j(_4Zq["ZM*W0var I: integer;
:LDO!ve^$A0begin51Testing软件测试网\3`6G8Os%`
for I:=2 to trunc(sqrt(n)) do
9CbV mh8\0if n mod I=0 then begin51Testing软件测试网3R5l|6kC&L2w
prime:=false; exit;51Testing软件测试网.?#Q.HZ+\&x
end;
%^? j$rAj)gLz*u H!h0prime:=true;51Testing软件测试网x`ATJ
end;
U?.N.i"|0
j2Dci$ZN;??0ac0B.判断longint范围内的数是否为素数(包含求50000以内的素数表):
0t[z"O+b!}0C |n|0procedure getprime;51Testing软件测试网C8u-mbM"v
var
tfo,H;dtC1eb0i,j:longint;51Testing软件测试网[ Q&e7e J^(q1al
p:array[1..50000] of boolean;
A3?T Y-rSW`~0begin51Testing软件测试网4b\-j1} n }[/n
fillchar(p,sizeof(p),true);
$tDB p Ve\0p[1]:=false;51Testing软件测试网mJ$cLh
i:=2;
~k9~x2}ruch0while i< 50000 do begin51Testing软件测试网#] O3Q` H+X.Z
if p[i] then begin51Testing软件测试网rigC:`6w!t MI
j:=i*2;51Testing软件测试网)f$}+c s-vV
while j< 50000 do begin
Af0qp;AM:S2['A0p[j]:=false;51Testing软件测试网 nBZ"^sO)S.u*s
inc(j,i);51Testing软件测试网X0aE y3j
end;
X]![\$P kFLd.p0end;51Testing软件测试网-IHb#|x-Q`,? U
inc(i);51Testing软件测试网1}iZ!dRS$}0K2Hy
end;51Testing软件测试网7f?d ^C0TF1~s
l:=0;51Testing软件测试网"SLGPK@Z.h
for i:=1 to 50000 do
$i_w/u#bhp%wX|0if p[i] then begin
"nVo-n1|2H0inc(l);pr[l]:=i;
.{;] z rZ%AeLF#d:D0end;
']-}v z zs0end;{getprime}51Testing软件测试网/GQ#{1[R |Nf"y+f

q7KJ;U"g1]5I1lA{m0function prime(x:longint):integer;
/e_fK#m A0var i:integer;
n@ sKyS+j0begin51Testing软件测试网5E:Tg)S3p1jMf
prime:=false;
vM8Og.j#J0for i:=1 to l do
H7N~5j c0if pr[i] >=x then break51Testing软件测试网:C.m$QUr+Q[
else if x mod pr[i]=0 then exit;51Testing软件测试网ou6tqmN
prime:=true;51Testing软件测试网 E lb9e;CRB+n
end;{prime}
!G3cCrG;v4}g B5@MpK0
C,a@G4zD02.
[]hxD7^7v YQ051Testing软件测试网P2Np;[Je%j
3.
0Kp/Qbj}E.\051Testing软件测试网1xw4x~WH
51Testing软件测试网1`k%H#Z]:bb
4.求最小生成树
bQ^5](l0A.Prim算法:51Testing软件测试网8GOpQj8X
procedure prim(v0:integer);
2}-_1e/~?1k o0var51Testing软件测试网P(VE_3j
lowcost,closest:array[1..maxn] of integer;51Testing软件测试网k;cG2PFp
i,j,k,min:integer;51Testing软件测试网$B0u/Co ?!v
begin51Testing软件测试网j-pla$ER6k)?
for i:=1 to n do begin
#v2kz%uDD;`g0lowcost[i]:=cost[v0,i];51Testing软件测试网pA4I$A(E0E.C ZR
closest[i]:=v0;
7}#B \-P `A0end;
C'Z OK!{0W0for i:=1 to n-1 do begin51Testing软件测试网 IJ w {;i*y
{寻找离生成树最近的未加入顶点k}
aYc!L5m0min:=maxlongint;
w zB)C/](RT&O6{(K0for j:=1 to n do51Testing软件测试网T`lC!v
if (lowcost[j]< min) and (lowcost[j]< >0) then begin
/p\,i-a_8N3t6D7l0min:=lowcost[j];51Testing软件测试网0e%J&A`HPi
k:=j;
B$SAOtLu0end;51Testing软件测试网'ZrabiU9IS
lowcost[k]:=0; {将顶点k加入生成树}
o { Cg(gv[q0{生成树中增加一条新的边k到closest[k]}
%q.Vrp2~ j R%y+gwP)a0{修正各点的lowcost和closest值}
m2S_[N @yKZ y0for j:=1 to n do51Testing软件测试网1^ mf/Gtt j[.d
if cost[k,j]< lwocost[j] then begin
mbFF%_,e9O0lowcost[j]:=cost[k,j];
:ZR]k&li,v0closest[j]:=k;
;B9K/m3z g$G%l _ z X-R0end;51Testing软件测试网"I/\)M:iuZ
end;51Testing软件测试网z*R,S8?K:J
end;{prim}
;F Q*S Q T3{0
yl"SF-f7Gq Q}H1M0B.Kruskal算法:(贪心)51Testing软件测试网2V'oV3t:kM O\#|w"E#U
按权值递增顺序删去图中的边,若不形成回路则将此边加入最小生成树。
kd4i#Os'o0function find(v:integer):integer; {返回顶点v所在的集合}51Testing软件测试网"]'F1t_~
var i:integer;51Testing软件测试网?*D6~(j:n
begin51Testing软件测试网iUK7IO!t0A
i:=1;51Testing软件测试网(zr9}w6y ?
while (i< =n) and (not v in vset[i]) do inc(i);51Testing软件测试网HQ(p7B^3m
if i< =n then find:=i else find:=0;
Oe@.aA~oK'te0end;51Testing软件测试网l4Up3X0S'?){f

\ [CFp)Pt$r0procedure kruskal;
m JZ*I(b*EG'U oQD+T0var
~8V7D*t7N1[$KD vd0tot,i,j:integer;51Testing软件测试网E.V'XwGm;K
begin51Testing软件测试网3DvF0Q*Hu"g
for i:=1 to n do vset[i]:=[i];{初始化定义n个集合,第I个集合包含一个元素I}
)E7w t6e)ljzRem0p:=n-1; q:=1; tot:=0; {p为尚待加入的边数,q为边集指针}51Testing软件测试网6Z/c ~} i8J6iD W
sort;
$}P3H7xlT"d5r0{对所有边按权值递增排序,存于e[I]中,e[I].v1与e[I].v2为边I所连接的两个顶点的序号,e[I].len为第I条边的长度}51Testing软件测试网a~,{v%Kea
while p >0 do begin
1XG1hNSPCH0i:=find(e[q].v1);j:=find(e[q].v2);
:|n?q5WK"Q0if i< >j then begin51Testing软件测试网%rM:h,u;y:bO9m+S
inc(tot,e[q].len);
mCw%\$XkP7[G0vset[i]:=vset[i]+vset[j];vset[j]:=[];51Testing软件测试网c:[.DX1Qnb
dec(p);51Testing软件测试网E"R C%V6\ e
end;51Testing软件测试网"G%^^/m5b
inc(q);
c^ _/?ln nf:O ER u0end;
+G!hyc(z2s9I6Gu/C0writeln(tot);
0w&K`0|E0end;51Testing软件测试网q*M;f C|0N

?"n1cM9]N)?r0
\2h7Y,Pi|#m*G7F Hj05.最短路径
Q B[k]~|v.标号法求解单源点最短路径:51Testing软件测试网4C9x U ?:]0Y$b\%fm
var51Testing软件测试网(] qj$Px5@*Qd2T
a:array[1..maxn,1..maxn] of integer;51Testing软件测试网tb[0g2Q
b:array[1..maxn] of integer; {b[i]指顶点i到源点的最短路径}
;FU*A'_Eq"U`^0mark:array[1..maxn] of boolean;51Testing软件测试网4^C |~)a
51Testing软件测试网/^7A ]p@Fs0U
procedure bhf;51Testing软件测试网9u^[ wg+I5`7{H5XE
var
M Rjd` [N lI2B)x0best,best_j:integer;51Testing软件测试网VB9?7j,F y+`w
begin
? C#wcwgPf|A0fillchar(mark,sizeof(mark),false);
)h#`'Z i1]w0mark[1]:=true; b[1]:=0;{1为源点}51Testing软件测试网,WN_h,Bm6U
repeat
x-J _Bl:cmR0best:=0;51Testing软件测试网x.U/Op$@
for i:=1 to n do
Z1sdk#G+hH0If mark[i] then {对每一个已计算出最短路径的点}51Testing软件测试网3U1l!HU:c
for j:=1 to n do
O(jM3z:Vyl(|0if (not mark[j]) and (a[i,j] >0) then
X W0u.~e:a&B0if (best=0) or (b[i]+a[i,j]< best) then begin
#k.y^.a,jQ~ C0best:=b[i]+a[i,j]; best_j:=j;
YxG#ex,i0end;
IX4J`b0if best >0 then begin51Testing软件测试网V7l [+nq}
b[best_j]:=best;mark[best_j]:=true;
)TdP |9XIJ0end;51Testing软件测试网.Wf)\5K? zBn*v
until best=0;
d|-g"i2~0end;{bhf}51Testing软件测试网v]rr1i$_#Z

(S#Q5_Ag oJ&]A0B.Floyed算法求解所有顶点对之间的最短路径:
G9AR1Slk5}0procedure floyed;51Testing软件测试网s!Q BWD(D
begin
K+_#^op7q y0for I:=1 to n do
Qv"u)fh$k9B@'@0for j:=1 to n do51Testing软件测试网HWFR]Vu4\3f
if a[I,j] >0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]表示I到j的最短路径上j的前驱结点}51Testing软件测试网4|$I%T/j vH
for k:=1 to n do {枚举中间结点}51Testing软件测试网b%Z }YU6^y;@o(P;a
for i:=1 to n do51Testing软件测试网Zr]+Q5M'v+h!^9YV
for j:=1 to n do
t*F@ BV8n$j J0if a[i,k]+a[j,k]< a[i,j] then begin
3}-?$G-p*T,Uh6r0a[i,j]:=a[i,k]+a[k,j];51Testing软件测试网7`l3_ P:gbW!O0J
p[I,j]:=p[k,j];
B[Wos*B})y0end;51Testing软件测试网^*q@1bVj;q8E
end;51Testing软件测试网brp XS[$N0lQV

nu*j'l[X ~0C. Dijkstra 算法:51Testing软件测试网$U8`!u:O7l_D7A b
类似标号法,本质为贪心算法。51Testing软件测试网(Z#W-ZL R|R
var51Testing软件测试网 Y$H9iTy.[_*Q7?|
a:array[1..maxn,1..maxn] of integer;51Testing软件测试网,M)w0H'z7u
b,pre:array[1..maxn] of integer; {pre[i]指最短路径上I的前驱结点}
5VP7_&{#N(l1x ?[0mark:array[1..maxn] of boolean;51Testing软件测试网&r%Qpi$HiM
procedure dijkstra(v0:integer);51Testing软件测试网3E6CV Y4D9I
begin51Testing软件测试网 ig$ny:K3D4yj@-_
fillchar(mark,sizeof(mark),false);51Testing软件测试网4zK r_O
for i:=1 to n do begin51Testing软件测试网7A'CTh Q#Y^ d
d[i]:=a[v0,i];51Testing软件测试网/[d L^;m^3f2bI E
if d[i]< >0 then pre[i]:=v0 else pre[i]:=0;51Testing软件测试网`p/H"c%yuc
end;51Testing软件测试网OoC*`%bB7t h?6HW
mark[v0]:=true;51Testing软件测试网k8lP G6po ~1kNi']
repeat {每循环一次加入一个离1集合最近的结点并调整其他结点的参数}
&Ew pA5A0min:=maxint; u:=0; {u记录离1集合最近的结点}
D.t+c(z V0for i:=1 to n do51Testing软件测试网%Z z d%X(C;LB'j
if (not mark[i]) and (d[i]< min) then begin
8po N j;Ob$m0u:=i; min:=d[i];51Testing软件测试网i/b)ql E l+Zx
end;51Testing软件测试网T?nf/E h
if u< >0 then begin51Testing软件测试网t3_.Ikby;qeR$j
mark[u]:=true;
9RQ7I,`"h%^,@ \u P @y0for i:=1 to n do51Testing软件测试网 VQ ^"[,@t]zd
if (not mark[i]) and (a[u,i]+d[u]< d[i]) then begin
;S!c*m5WlG0d[i]:=a[u,i]+d[u];
#Sa8cDCe0pre[i]:=u;
U Zef1R'~ N)^b0end;51Testing软件测试网`3pHr~0C$M9X#A
end;51Testing软件测试网0b J8Z4qc!v`
until u=0;
)Et,s^;Ke0end;
6tR2Lk+L QCI0
d,N(FY-s2OJ8C'T0D.计算图的传递闭包
rv6v!}Gy6s B&Li0Procedure Longlink;51Testing软件测试网"LY^ Y7^&?8J)OZ z9g*SC
Var51Testing软件测试网4c NBsN
T:array[1..maxn,1..maxn] of boolean;
2h\2CP*r0Begin
;v],Gx%Ax3V L^l0Fillchar(t,sizeof(t),false);51Testing软件测试网9]2j eu ]\w0L
For k:=1 to n do51Testing软件测试网+IBl9Z"U@
For I:=1 to n do
#d.mPP4O/S!LN ^7M0For j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);
L%{q"F,ouq?(o0End;
E$pA O[7u']$IX051Testing软件测试网9\"VUd~(nm'Zd

P#oeE+YR1Q~#AN06.0-1背包问题(部分背包问题可有贪心法求解:计算Pi/Wi)
5WU?"xoV:q*`0数据结构:51Testing软件测试网XQ)Sj1ru2h
w[i]:第i个背包的重量;51Testing软件测试网L:`;fl a!F I7@0{6F7W
p[i]:第i个背包的价值;
T8`Gk(r+No0(1)0-1背包: 每个背包只能使用一次或有限次(可转化为一次):
;[ ~v V$\8D0A.求最多可放入的重量。
+v#r4H9}O!E4A0NOIP2001 装箱问题
^X7dRn0有一个箱子容量为v(正整数,o≤v≤20000),同时有n个物品(o≤n≤30),每个物品有一个体积 (正整数)。要求从 n 个物品中,任取若千个装入箱内,使箱子的剩余空间为最小。51Testing软件测试网0e/XN1D5zDx
l 搜索方法
&Y;z$K` l5H;Bq0procedure search(k,v:integer); {搜索第k个物品,剩余空间为v}
Q*FV0w R j'~0var i,j:integer;51Testing软件测试网\KABi Z"\y6a
begin
I#V#I!X],t;x"f0if v< best then best:=v;
ZYhw/c#MR2pt0if v-(s[n]-s[k-1]) >=best then exit; {s[n]为前n个物品的重量和}51Testing软件测试网9_}8c%n cA b6Z
if k< =n then begin51Testing软件测试网4OG_qR;bOvja
if v >w[k] then search(k+1,v-w[k]);51Testing软件测试网 T_I8~d C2i
search(k+1,v);
qz N#z?pA0end;51Testing软件测试网2x4m,O^"A E/Hg
end;
h1` U9df}&`-J051Testing软件测试网3i1K5in"}y,{+W@)}
l DP
#J)hm ]F,KL j~ q9z0F[I,j]为前i个物品中选择若干个放入使其体积正好为j的标志,为布尔型。51Testing软件测试网C[ L+zBu,|%?+hC
实现:将最优化问题转化为判定性问题
v;O V(vso|0F[I,j]=f[i-1,j-w[i]] (w[I]< =j< =v) 边界:f[0,0]:=true.51Testing软件测试网JD.dE8~@;E[
For I:=1 to n do51Testing软件测试网u'R2q4b%V'Q'T Cr*p4@\
For j:=w[I] to v do F[I,j]:=f[I-1,j-w[I]];51Testing软件测试网%j,~rDh j y`8E
优化:当前状态只与前一阶段状态有关,可降至一维。
Z#v8s7^)\0F[0]:=true;
8x"Kt/Z)J;[b0For I:=1 to n do begin51Testing软件测试网&P R+K `hO4ae'?
F1:=f;51Testing软件测试网d+bt*RZ@S7d"X
For j:=w[I] to v do51Testing软件测试网M-lZ(} E-l
If f[j-w[I]] then f1[j]:=true;51Testing软件测试网6uk9F7K3E
F:=f1;51Testing软件测试网4s%Br4Fe.p8K]
End;
3_,SK$v6S-Qf5C;M0
SrX\0Ee e)?0B.求可以放入的最大价值。51Testing软件测试网.G's/X1u!G`D
F[I,j]=51Testing软件测试网.D-nT7{Qye'X]^
51Testing软件测试网+^bG2EzoZ8I#~G
51Testing软件测试网[G$fq dil)xF$cj ~1Q
C.求恰好装满的情况数。51Testing软件测试网4B2W]K1aow

*d[Wg4c6n5Vb j0 
:Z T#?.i5e DT*k0
~B7n7QC xoo"L0(2)每个背包可使用任意次:
^#?eYR4`0A.求最多可放入的重量。
WKT*S] U1jn/X0状态转移方程为51Testing软件测试网8cM$@z^%x2w
f[I,j]=max{f[i-w[j]
2oiEW)N&NH:H(u0
&wjP!b7`QU NH%MO0 
'j@a6@7r9p-~051Testing软件测试网+uy6u:Ksc'oF }e

? bY8k2xY1l0B.求可以放入的最大价值。
j\!q1`d7N Qp0USACO 1.2 Score Inflation51Testing软件测试网t Ep-M \ oI
进行一次竞赛,总时间T固定,有若干种可选择的题目,每种题目可选入的数量不限,每种题目有一个ti(解答此题所需的时间)和一个si(解答此题所得的分数),现要选择若干题目,使解这些题的总时间在T以内的前提下,所得的总分最大,求最大的得分。
hB%Un urU s0*易想到:51Testing软件测试网E/k YgP"`;i
f[i,j] = max { f [i- k*w[j], j-1] + k*v[j] } (0< =k< = i div w[j])51Testing软件测试网?4p`o8o~
其中f[i,j]表示容量为i时取前j种背包所能达到的最大值。51Testing软件测试网on,jn)u{e3n
*优化:
| E~i;F Lx0Begin
E-z5D-R8Mw8[0FillChar(problem,SizeOf(problem),0);
*Pre1]V0Assign(Input,'inflate.in');51Testing软件测试网L(g.PjLq"wH-V
Reset(Input);
a9c4}WuY$J.] zv0Readln(M,N);
*TG Px n*F-x} [j0For i:=1 To N Do51Testing软件测试网W%P'TN E(h1C
With problem[i] Do51Testing软件测试网b$CI/uaB#M/mk
Readln(point,time);
(W2z C3n[0? p H3Y0Close(Input);51Testing软件测试网.hl1Mp*DK#?,Q-M%G
51Testing软件测试网9zqG `;B{a k
FillChar(f,SizeOf(f),0);51Testing软件测试网(]Ke;S$F
For i:=1 To M Do
+YgxF${J%A q:n0For j:=1 To N Do51Testing软件测试网~B#Ru5zt
If i-problem[j].time >=0 Then51Testing软件测试网| Y2N)~Byc]@
Begin51Testing软件测试网F!}oc CB/g q
t:=problem[j].point+f[i-problem[j].time];
-S)M6A*a5R0If t >f[i] Then f[i]:=t;
TT C7jn6{0End;51Testing软件测试网,x ^.C F*s ziE

bX2~#`D+I"H/y"sp0Assign(Output,'inflate.out');51Testing软件测试网n[$d]pk
Rewrite(Output);
(gi9OX+g0Writeln(f[M]);
(t.G xq'B `r0Close(Output);
h\v g,K#cg3ID/K0End.51Testing软件测试网"fY[ n2u0p't`.w:zi,f~
C.求恰好装满的情况数。
-gStd4Hx0Ahoi2001 Problem2
u{ `3q&vr|m0求自然数n本质不同的质数和的表达式的数目。51Testing软件测试网?1GxU@q3Z
思路一,生成每个质数的系数的排列,在一一测试,这是通法。
nX7k-P)f*f0procedure try(dep:integer);51Testing软件测试网8c EZW5y+Iik ^'d
var i,j:integer;
fj;Ord} Zj0begin51Testing软件测试网&k}}~1EC1{){7}
cal; {此过程计算当前系数的计算结果,now为结果}51Testing软件测试网"o:b%S&U,|s.KL
if now >n then exit; {剪枝}51Testing软件测试网/g)r#O"reM3n#ks(X
if dep=l+1 then begin {生成所有系数}
"O"Tm2C*k0cal;
q zMe4T!f xhL4K0if now=n then inc(tot);51Testing软件测试网;fEj:_+j
exit;51Testing软件测试网Od3S,Eu7{7d
end;
m{ e NL uu ^J0for i:=0 to n div pr[dep] do begin51Testing软件测试网 ^ss[Gj&^
xs[dep]:=i;
R"RwdD)C8g[o0try(dep+1);
M*D'X1eL0_)F.k-d0xs[dep]:=0;
\w;|KR6s0end;51Testing软件测试网p"vh%M'~
end;
gu a@ L/c0g4L[051Testing软件测试网#|C%e6_%e+p w:`l&V
思路二,递归搜索效率较高
G-Z1p | R3Is \.Z~0procedure try(dep,rest:integer);51Testing软件测试网n_UK^7K
var i,j,x:integer;51Testing软件测试网(T/m2Xlo"e2t0A c
begin51Testing软件测试网(R{? qZ
if (rest< =0) or (dep=l+1) then begin51Testing软件测试网(]]'_.x*S*F:bZ
if rest=0 then inc(tot);
Il-MLAI0exit;51Testing软件测试网8~9@?wY/G
end;
j^$Qg2r ]+~W0G0for i:=0 to rest div pr[dep] do51Testing软件测试网UBK0j#?!Zg
try(dep+1,rest-pr[dep]*i);51Testing软件测试网vV%ZwyM4DSQr
end;51Testing软件测试网#l[?cga+Xr;N
51Testing软件测试网A:GGrO9c
思路三:可使用动态规划求解51Testing软件测试网#c:tn5n.a!Z0Q[[j
USACO1.2 money system
tJL%Nkw0V个物品,背包容量为n,求放法总数。
W9y;F P"eI C0转移方程:
Oid9a:[ PFJ051Testing软件测试网 z^/_ tGQ{
Procedure update;
n5g1F*HQ+A:K?2Y0var j,k:integer;51Testing软件测试网;\wR$y!SHeVE
begin51Testing软件测试网 e1T9h3hQ%^Tr
c:=a;
w$N?i-V h0for j:=0 to n do
hqC L;iPc h;ruJ0if a[j] >0 then
8~.i Z5G/DC0for k:=1 to n div now do
a"kO9YF[u*|9]0if j+now*k< =n then inc(c[j+now*k],a[j]);
+F ul8xT7cg0a:=c;
'i] fg"u;m%N t$j0end;51Testing软件测试网hT:] g6Tm[6i
{main}51Testing软件测试网fMA L/UE/~?
begin
3H6_-g'C`0read(now); {读入第一个物品的重量}51Testing软件测试网(r$vp_4R m|6bY
i:=0; {a[i]为背包容量为i时的放法总数}
,xy9uw(d0while i< =n do begin
*Rm0eC,b5q(gb0a[i]:=1; inc(i,now); end; {定义第一个物品重的整数倍的重量a值为1,作为初值}
.?0?&p*~@vdY,ez0for i:=2 to v do51Testing软件测试网h ` \zQw6V
begin51Testing软件测试网W(i6p L7j%j fk
read(now);
7h*T1vB4h/J:^0update; {动态更新}51Testing软件测试网H#C.sKsWG.n
end;51Testing软件测试网?Q.]$|b V J y&}2}dCRS
writeln(a[n]);
Q$e1H;ZE0
M&E| s(bf;Y){*\+v07.排序算法51Testing软件测试网O.nRUtU3Tv
A.快速排序:51Testing软件测试网%JA(}Gngz
procedure sort(l,r:integer);51Testing软件测试网2ImS m2Z"Q~ nke
var i,j,mid:integer;51Testing软件测试网Y(f&l8GDcv?*N.Z
begin51Testing软件测试网7r#l'E,?6Q ])Qc
i:=l;j:=r; mid:=a[(l+r) div 2]; {将当前序列在中间位置的数定义为中间数}51Testing软件测试网)^ Bi9?'J-aD2Y+h
repeat
cN)v0G+J8\` sr,M0while a[i]< mid do inc(i); {在左半部分寻找比中间数大的数}
;`XS[XN^!av/i0while mid< a[j] do dec(j);{在右半部分寻找比中间数小的数}51Testing软件测试网,k@4X!w7M
if i< =j then begin {若找到一组与排序目标不一致的数对则交换它们}51Testing软件测试网7s Bd*Z1~i~8\x
swap(a[i],a[j]);
(K%rkMw D-t.T0inc(i);dec(j); {继续找}51Testing软件测试网L%I[DY
end;51Testing软件测试网Fq6Vz ^
until i >j;51Testing软件测试网gKqJ(H`g
if l< j then sort(l,j); {若未到两个数的边界,则递归搜索左右区间}51Testing软件测试网 kB7DW%OkA T;o
if i< r then sort(i,r);51Testing软件测试网V&YZ%M7j
end;{sort}
"pZ#AUE0J0 
+P5\eqb;dU#a{j051Testing软件测试网R x8YY.@/e%b3[:Oi$QM%{
B.插入排序:51Testing软件测试网)m4z3r@ `9g'OA
procedure insert_sort(k,m:word); {k为当前要插入的数,m为插入位置的指针}51Testing软件测试网9^^0A$jCe f
var i:word; p:0..1;51Testing软件测试网B,P)P,H Y8}
begin
$Sh8bb n(IX0p:=0;51Testing软件测试网8d.d4[6\ D"i)C:a
for i:=m downto 1 do51Testing软件测试网'Eh4_,@,U-^r k
if k=a[i] then exit;
%U#O&eI]0repeat51Testing软件测试网 Ds duOI#y
If k >a[m] then begin
3]O'z!f){Q0a[m+1]:=k; p:=1;51Testing软件测试网5I+d K!fXt*x*V/K
end51Testing软件测试网/C7`'K7i3f!g&Z
else begin
vP lh j-Ul \0a[m+1]:=a[m]; dec(m);
Tq oI-r~1c.ok.~G0end;51Testing软件测试网3p\pl9P@~
until p=1;
^@ziD0w)V+Kl)G0end;{insert_sort}51Testing软件测试网`3i@+bI DOMG `
l 主程序中为:51Testing软件测试网1oQWe7Z h%jN
a[0]:=0;51Testing软件测试网;[/WG N?.\r*Z
for I:=1 to n do insert_sort(b[I],I-1);51Testing软件测试网g V4I-`V]$[N

&yP7|'W$o5U){gJ3Ex:P0C.选择排序:51Testing软件测试网2[g pXu#|k1d
procedure sort;
Qs3m j)Hu pR&d!?!Z0var i,j,k:integer;51Testing软件测试网#nKO0r*r
begin
1q&W4J:? Z$in(^0for i:=1 to n-1 do begin51Testing软件测试网 q[4Ru x1wCn|
k:=i;51Testing软件测试网|oQ7aWDg6X\/X
for j:=i+1 to n do
s)r&flmuyIo0if a[j]< a[k] then k:=j; {找出a[I]..a[n]中最小的数与a[I]作交换}51Testing软件测试网i7W(lp:u]j!~3w?
if k< >i then begin51Testing软件测试网)c7Uvn g#k{1n
a[0]:=a[k];a[k]:=a[i];a[i]:=a[0];
R!xb$Hf0H0end;51Testing软件测试网g6G M2m8]0K Qi'ku"K X
end;51Testing软件测试网)m x x1`&H6y8m
end;
5fV/Zc;T9w \0
Y5l#Ddx3^tS0D. 冒泡排序
'p#G#{2?[zs X0procedure sort;
%yQe \'a7NV GL0var i,j,k:integer;51Testing软件测试网]O-a(t6}c/Mh \
begin51Testing软件测试网hI:d@1? a
for i:=n downto 1 do51Testing软件测试网2r}D-M4y1@
for j:=1 to i-1 do51Testing软件测试网7KV%y#r"v"mo5K he5j^
if a[j] >a[i] then begin
1CVF8LRu*d3V0a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];51Testing软件测试网*i\G.t J
end;
_z.El(Gy&D7p0end;
%oQg]P,DN0
z2K J4h?U0E.堆排序:51Testing软件测试网[bT3L4q:}M;f
procedure sift(i,m:integer);{调整以i为根的子树成为堆,m为结点总数}51Testing软件测试网 Q^@0g,v7B/[9Z
var k:integer;51Testing软件测试网A)vS3_d/a
begin
8`T @9N m]0a[0]:=a[i]; k:=2*i;{在完全二叉树中结点i的左孩子为2*i,右孩子为2*i+1}
.^@ D(D,AV3V{t0while k< =m do begin
mMq ~DQ&rD0if (k< m) and (a[k]< a[k+1]) then inc(k);{找出a[k]与a[k+1]中较大值}51Testing软件测试网4C+U~|`
if a[0]< a[k] then begin a[i]:=a[k];i:=k;k:=2*i; end51Testing软件测试网 ?.d c2A i c
else k:=m+1;
E3f)@1Hg?;GV0end;51Testing软件测试网^ ^S|2i p'}
a[i]:=a[0]; {将根放在合适的位置}
+s&Y5xi-}0end;51Testing软件测试网"VQzT7L*U
51Testing软件测试网3J&eA4D/VJ#oL
procedure heapsort;
k#R w}sy(uO"^0var51Testing软件测试网P W_ dh/Tu$Aq
j:integer;
.U'Ux(h9H%Sl0begin51Testing软件测试网3v_?+mah'~L0U
for j:=n div 2 downto 1 do sift(j,n);51Testing软件测试网+m]5z;_U_
for j:=n downto 2 do begin51Testing软件测试网OJWi-T4h
swap(a[1],a[j]);
*j8Z7ehyf0sift(1,j-1);51Testing软件测试网#mts c7a)\&R
end;51Testing软件测试网B6]4\H}_@#I
end;51Testing软件测试网8j3H9KBH{6Gd*OB

~*YN,C2_V{:h7| T}0F. 归并排序
3zL4a.F)^Ry6W0{a为序列表,tmp为辅助数组}51Testing软件测试网Q"yZ5{-d
procedure merge(var a:listtype; p,q,r:integer);51Testing软件测试网}$Ha oX)a9g@(n
{将已排序好的子序列a[p..q]与a[q+1..r]合并为有序的tmp[p..r]}51Testing软件测试网4n(T C C4O&iJ
var I,j,t:integer;
h%Rd`'?I0tmp:listtype;51Testing软件测试网:UG_jS!r ^-\
begin
D5I:ePn{0t:=p;i:=p;j:=q+1;{t为tmp指针,I,j分别为左右子序列的指针}51Testing软件测试网*EB+l(n.XV5]0g
while (t< =r) do begin
%c @.g R5l3a5G!t$h0if (i< =q){左序列有剩余} and ((j >r) or (a[i]< =a[j])) {满足取左边序列当前元素的要求}51Testing软件测试网}%_3b1tJI4^
then begin51Testing软件测试网W@$ZTU fgU+u
tmp[t]:=a[i]; inc(i);51Testing软件测试网.p u0x h{@ p(ve#Wd&|
end
N H!N3ib(zkL5p EJi0else begin51Testing软件测试网&iah~x&su
tmp[t]:=a[j];inc(j);51Testing软件测试网$v8z1M^ { [Z
end;
X-d(Y$E^lh [U v:Bl0inc(t);
i2xn(Xj_0end;
+^'X?JA|1gJ0for i:=p to r do a[i]:=tmp[i];51Testing软件测试网t*]^(E?Q
end;{merge}51Testing软件测试网S\k)q-]V,KY@

{/L CEa/dVZU-w0procedure merge_sort(var a:listtype; p,r: integer); {合并排序a[p..r]}
3v(G,})P'g/]XM3_x0var q:integer;51Testing软件测试网cW"Q9EEtL~0F
begin51Testing软件测试网(i5~}(u0|hp$K
if p< >r then begin
g+`+{bLq,Ar(h)k:J"^0q:=(p+r-1) div 2;51Testing软件测试网&fUeS#GS?
merge_sort (a,p,q);51Testing软件测试网z P;i5q8` q H:`t
merge_sort (a,q+1,r);51Testing软件测试网g)Y W%v&J
merge (a,p,q,r);
{ Ke&t5B'y7l0end;
%z-v-S"a3AW0end;
*S7JDnx2Sp[ X0{main}
LP vW:I]'F0begin51Testing软件测试网g0Mf oUS^_-x
merge_sort(a,1,n);
ME{k7q5lX0end.51Testing软件测试网T)w ` Q$Xp8PH,o8e
51Testing软件测试网fqc\A,{ B
51Testing软件测试网.C2|M[\ a
G.基数排序
9W o%_ \;zq0思想:对每个元素按从低位到高位对每一位进行一次排序51Testing软件测试网,MRH s!I:Ol Q8z
51Testing软件测试网:?;t'@wX qD7C8j"b
51Testing软件测试网0j M UP0y
8.高精度计算
nl^Te)B@ J`"X0A.51Testing软件测试网PE{8_1N Ja*T
B.51Testing软件测试网{b_!| \-PL+e9H3yd
C.51Testing软件测试网A%_ g3c_ @ jM
D.
!HAv(Koa0
E'LY;BN+_09.树的遍历顺序转换51Testing软件测试网xL.t8e(gLr^ `
A. 已知前序中序求后序
4x@3c8}*B&h^0procedure Solve(pre,mid:string);
%C9Cz#k&QOI dt#w"r5o0var i:integer;
;i:kN1MWa0s7q0begin51Testing软件测试网a].Q%T4vC2E#m1G%^1v
if (pre='') or (mid='') then exit;51Testing软件测试网$j0r:}M? fF}
i:=pos(pre[1],mid);51Testing软件测试网n-\)JU+NH9?c
solve(copy(pre,2,i),copy(mid,1,i-1));
xgUTB| Ac0solve(copy(pre,i+1,length(pre)-i),copy(mid,i+1,length(mid)-i));51Testing软件测试网8SlOW5na+J W+^/Jh#w%`
post:=post+pre[1]; {加上根,递归结束后post即为后序遍历}
u#IU2m_}Dfef!Q0end;51Testing软件测试网jqXya*j*B M{?h
51Testing软件测试网 N-Cc5{#V{'HRy
B.已知中序后序求前序51Testing软件测试网S6^.K5D }/j!b@
procedure Solve(mid,post:string);
4k cE!YZvM3\Y7g0var i:integer;
L:_pw,dO)i-~7E0begin51Testing软件测试网D#\3l%tQ/}
if (mid='') or (post='') then exit;
8y} ky{?^%Z8E!B0i:=pos(post[length(post)],mid);51Testing软件测试网s#|qb;{q
pre:=pre+post[length(post)]; {加上根,递归结束后pre即为前序遍历}
K*I%B-K~0solve(copy(mid,1,I-1),copy(post,1,I-1));
.f'X2B4Ab.K0O&T0solve(copy(mid,I+1,length(mid)-I),copy(post,I,length(post)-i));51Testing软件测试网m#D&s@8d0S6u
end;
,a l4JQS2|cMk]0
6F#\NAR0C.已知前序后序求中序
e.mZM W4L0
7yiYI @ K0function ok(s1,s2:string):boolean;
"qLcJ N:C0var i,l:integer; p:boolean;51Testing软件测试网oxy6V(A&q,\^
begin51Testing软件测试网 V2s}.ff![
ok:=true;51Testing软件测试网 K-b ~1Y8?lC
l:=length(s1);
0OkY'@z"HB3~0for i:=1 to l do begin
t|zr_8m0NS0p:=false;51Testing软件测试网_?RVV){
for j:=1 to l do51Testing软件测试网y2h"Too&[q
if s1[i]=s2[j] then p:=true;
;qmW#t f,b3D0if not p then begin ok:=false;exit;end;
q)lQE!^"aoB0end;
7z v| f D:W0end;51Testing软件测试网*G2`@$@0@?

%K)aZ%?GtQ$S"fY;l d0procedure solve(pre,post:string);51Testing软件测试网Zwv0wq#~"Q7E
var i:integer;51Testing软件测试网s7n t"@N7Y
begin
b`kY:ra6W0if (pre='') or (post='') then exit;
(i}Glt6?mI0i:=0;51Testing软件测试网a~%Uh;it,Qw2a
repeat
X8j P O1N6c$C}0inc(i);51Testing软件测试网5yGz]Rj3G,~4m
until ok(copy(pre,2,i),copy(post,1,i));51Testing软件测试网"rr0Fa s^(?8} Rv S
solve(copy(pre,2,i),copy(post,1,i));51Testing软件测试网|.V"uL k'm^9ZOB
midstr:=midstr+pre[1];
&r1A.k%u)F/SXX0solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1));51Testing软件测试网/Ic+PEJ1@
end;
E,C4OQ.xwX C*k8]0
7ueGa0n%b010.求图的弱连通子图(DFS)51Testing软件测试网h,vL_i,~R M%r
procedure dfs ( now,color: integer);
yX-|/V6lG0?x0begin
0x utq"X%t3Y9Wy-D0for i:=1 to n do
*[D4lbl,I0if a[now,i] and c[i]=0 then begin51Testing软件测试网1c_-g^wb u M
c[i]:=color;51Testing软件测试网CMT~Es
dfs(I,color);
k/L.y:ZtM8Ps0end;
`4?f:jsO+]@0qp0end;
9T/Fq3z8L J6E"?"a0
C3I/C,lm051Testing软件测试网Ot4s4w_vr~
11.拓扑排序51Testing软件测试网4X\[C a_,T
寻找一数列,其中任意连续p项之和为正,任意q 项之和为负,若不存在则输出NO.
zz9k"a8J bP051Testing软件测试网[ b F&|Cp$C_
51Testing软件测试网 H}2p(X7mg:_5E
12.进制转换51Testing软件测试网 g;g?B%A4?{A N9J7oE
A.整数任意正整数进制间的互化
L5Z.r O x?9\051Testing软件测试网E0f5R}T
NOIP1996数制转换
8E-Gk/C5KUK-YU a0设字符串A$的结构为: A$='mp'
Fp6Yo(Ft6^0其中m为数字串(长度< =20),而n,p均为1或2位的数字串(其中所表达的内容在2-10之间)51Testing软件测试网 f&b]4F-}4N
程序要求:从键盘上读入A$后(不用正确性检查),将A$中的数字串m(n进制)以p进制的形式输出.51Testing软件测试网"hvs \;\ ` v4F e
例如:A$='48< 10 >8'51Testing软件测试网1nh1~d-h1F nNC
其意义为:将10进制数48,转换为8进制数输出.51Testing软件测试网2T u.XVz_
输出结果:48< 10 >=60< 8 >
)Z1T{C1j8@ zj L^051Testing软件测试网a_6R?Fq)M}
B.实数任意正整数进制间的互化
Lm Y*k]0C.负数进制:51Testing软件测试网z0j"}:o`+z
NOIP2000
TUx%]0L;[0设计一个程序,读入一个十进制数的基数和一个负进制数的基数,并将此十进制数转换为此负 进制下的数:-R∈{-2,-3,-4,....-20}51Testing软件测试网Q{'M#_Hq z-i
51Testing软件测试网 T ?-G3r x4F~{
13.全排列与组合的生成
R3F9T7k6D{0排列的生成:(1..n)51Testing软件测试网1t!~$V0o+TG)SJ
procedure solve(dep:integer);51Testing软件测试网3nt gH2WN
var51Testing软件测试网/{,]8KR&R
i:integer;
wwXH#[/C4e0begin
b*ey:[G,InbT ^3@{0if dep=n+1 then begin writeln(s);exit; end;51Testing软件测试网ece/wo:r x;e H
for i:=1 to n do51Testing软件测试网5hq6{ S D,s3Jb*A
if not used[i] then begin
yA7z@)x-F%o Z0s:=s+chr(i+ord('0'));used[i]:=true;51Testing软件测试网~ozRO$Aa_,JG
solve(dep+1);
(P,m&eGs]5ZTT0s:=copy(s,1,length(s)-1); used[i]:=false;51Testing软件测试网p:hjuuEC
end;51Testing软件测试网Ky"a] |M5P
end;51Testing软件测试网uVl(rGw,BV
组合的生成(1..n中选取k个数的所有方案)
!?*DPmPv D'q5IF0procedure solve(dep,pre:integer);51Testing软件测试网 mEa S&?.DK
var51Testing软件测试网/TiL t/efWB:x
i:integer;
:Q qs\(a0begin51Testing软件测试网\W'S| D7^#u
if dep=k+1 then begin writeln(s);exit; end;51Testing软件测试网;ZE1V;|F4_ec qL)D
for i:=1 to n do51Testing软件测试网8|5OC&S g
if (not used[i]) and (i >pre) then begin
3OS^/k1N*b$E;O0s:=s+chr(i+ord('0'));used[i]:=true;
Tf!@3] k YHa]0solve(dep+1,i);51Testing软件测试网8@j[`c%n[m e M
s:=copy(s,1,length(s)-1); used[i]:=false;51Testing软件测试网9zP%["`8cE
end;51Testing软件测试网w:X8@}&zZ6|
end;
d gz}6?8b3@8p"M0
T @ [Z!t$}{%i0 
j_xo-f051Testing软件测试网Yn3ls y"w
14 递推关系51Testing软件测试网x ^-u2EH rx;]
计算字串序号模型
Y4VdV d|0USACO1.2.5 StringSobits
U I/l|U+T;q0长度为N (N< =31)的01串中1的个数小于等于L的串组成的集合中找出按大小排序后的第I个01串。51Testing软件测试网0T0f_.D6v`C#[#x
51Testing软件测试网"b#Yu9o@7ZLw(r,t

0J6H;C@,eb$]g0数字划分模型
-|"Bmt(vwW0Nq0R0*NOIP2001数的划分
!C;r%fQ&D6_!yV0将整数n分成k份,且每份不能为空,任意两种分法不能相同(不考虑顺序)。
f!d/P:Fh;K6r0d[0,0]:=1;
a)r!Y(c,|2A0for p:=1 to n do51Testing软件测试网L}1F&Fs"Kg(|
for i:=p to n do
2Fo0Hu\+H4Z(nDQ*e0for j:=k downto 1 do inc(d[i,j],d[i-p,j-1]);
9d!o`fo5u1L"N0writeln(d[n,k]);
A7bC}`0
8l#}Z!P `(Q ]&K0*变形1:考虑顺序51Testing软件测试网 q|s Ee-Kq
d[ i, j] : = d [ i-k, j-1] (k=1..i)51Testing软件测试网P~4bp5z;HHB
*变形2:若分解出来的每个数均有一个上限m
0K%T{ N/XH u0d0d[ i, j] : = d [ i-k, j-1] (k=1..m)51Testing软件测试网.iXG*F iq1{#Vds

n}PF ]7\051Testing软件测试网M3GO1RIAX|9V
15.算符优先法求解表达式求值问题
'Vm-~'v fR~D4]0const maxn=50;
7W/Hsw,J"GW0var
3d1~O I!n+pVj\ C0s1:array[1..maxn] of integer; {s1为数字栈}
9g/N,cMdi u0s2:array[1..maxn] of char; {s2为算符栈}51Testing软件测试网E#R M o2ItZ
t1,t2:integer; {栈顶指针}
7h)Xh)u'y9~ WH#n0
8w AC]NV j0procedure calcu;51Testing软件测试网fG8Td&?geV
var51Testing软件测试网n8Tl.n~;W
x1,x2,x:integer;
-b m Qx1@N/s0p:char;51Testing软件测试网H_3H&e7V
begin
&^4MO})j%~$MpB/z0p:=s2[t2]; dec(t2);51Testing软件测试网$Awj/lVk6p/W4c
x2:=s1[t1]; dec(t1);51Testing软件测试网"r+P`3d[w(j
x1:=s1[t1]; dec(t1);
;G Ru1VAC9J4M(t0case p of51Testing软件测试网"aj1b/^k5Y&lNj
'+':x:=x1+x2;
~3M%R}l6a0'-':x:=x1-x2;51Testing软件测试网b6bY&E b-G9^?8`1V
'*':x:=x1*x2;
%G Y;N#c] H!@ tAt0'/':x:=x1 div 2;51Testing软件测试网Zw }u*G zW7G!em
end;
)o2]*\2uX0Gj4g0inc(t1);s1[t1]:=x;51Testing软件测试网#vKdn3s2l*}
end;
h+jl5wQa&Ha*i0
&lxHR!db0procedure work;
jZ] E&y.e)p t`0var c:char;v:integer;
5XF1r%@-t2x`0begin51Testing软件测试网y`7r,wMZ
t1:=0;t2:=0;51Testing软件测试网9X1Ya}f2OSW-MQH
read&copy;;51Testing软件测试网} HE,`GU
while c< >';' do
3pvs"W,L8i!i0case c of
D;qS!|$[A'] Nd0'+','-': begin51Testing软件测试网!wH;S%?/G GJ
while (t2 >0) and (s2[t2]< >'(') do calcu;
aerU3]A9R/t0\0inc(t2);s2[t2]:=c;
sxWU$CdE0read&copy;;51Testing软件测试网Y8w7Z5C-Vj
end ;
8^)VV(a:{:AL+cQ0'*','/':begin51Testing软件测试网-aWz%d&sl J2U2Q
if (t2 >0) and ((s2[t2]='*') or (s2[t2]='/')) then calcu;
aa |sjt V0inc(t2);s2[t2]:=c;51Testing软件测试网K,aN7EnHU2P R wo
read&copy;;51Testing软件测试网} yR&|CoB
end;
b8~-z2B0P U#^0'(':begin inc(t2); s2[t2]:=c; read&copy;; end;
,eY0\-Ad0')':begin51Testing软件测试网ZL+e v8hj-^/|a:g
while s2[t2]< >'(' do calcu;51Testing软件测试网 lo T:lh+P
dec(t2); read&copy;;
I Di'M*aO7fb0end;
Bn$L `BdvKtb0'0'..'9':begin
?H S c ]B0v:=0;51Testing软件测试网F0fR`Usl'sam
repeat51Testing软件测试网[g"sr3]+R8Wc
v:=10*v+ord&copy;-ord('0');
SlQaZ6g'V`4N0read&copy;;
nh8M*N(WH0until (c< '0') or (c >'9');
)s Y"Lv#?0inc(t1); s1[t1]:=v;
s[d8o&t*MB_3q0end;51Testing软件测试网^p3]@SHB
end;51Testing软件测试网c^2~,|C6D+uM
while t2 >0 do calcu;
r1Qj3z1Nsv0writeln(s1[t1]);
P['r'ci#vl/Z*]0end;51Testing软件测试网a9}]'j3u{8G
51Testing软件测试网 J3?1{d3I|@%[
16.查找算法51Testing软件测试网!@P@"}!\-[_Zfo.]
折半查找51Testing软件测试网L&E8jc X%D(R4uH8W;BW
function binsearch(k:keytype):integer;51Testing软件测试网!J~*jI'O
var low,hig,mid:integer;
C`*qxn Cz(Nfo7]0begin51Testing软件测试网Z`h;k|C%CI2y
low:=1;hig:=n;51Testing软件测试网J"Y,Hv@sESh
mid:=(low+hig) div 2;
yq@8YH S0while (a[mid].key< >k) and (low< =hig) do begin
3W!h$f"H%t e;V$i YSb0if a[mid].key >k then hig:=mid-1
D-Ys/I4D7`-y6N,z~I.f#n0else low:=mid+1;
z7I3Zmc)W8U${*i u0mid:=(low+hig) div 2;
'MKif[N&C4}0end;
3kSQ!{nd%_4A!j0if low >hig then mid:=0;51Testing软件测试网4Zm,hs&M
binsearch:=mid;
'h v+AiTO@1Xg0end;
)HA;aZ{b0树形查找51Testing软件测试网O t)J?-[0@p:wQ}
二叉排序树:每个结点的值都大于其左子树任一结点的值而小于其右子树任一结点的值。
jg+Q8i5n+^z~z0查找
;@ G-ch4O'L0function treesrh(k:keytype):pointer;51Testing软件测试网-A8r:JN$R4Mjg
var q:pointer;51Testing软件测试网}.RL9n1T
begin51Testing软件测试网*N*sy8St8N-l0@z
q:=root;
O/FcZ YfC0while (q< >nil) and (q^.key< >k) do51Testing软件测试网 ~+a$M,v.yiy(Ep Q
if k< q^.key then q:=q^.left51Testing软件测试网 DiOXDQP4`i:Uy
else q:=q^.right;
p'a pRP(}_,C0treesrh:=q;
1R%se Q9d h0end;
&h&U/~/A4Qxtm4L0
9@"g+DP,B ?0
$DYLQkv017.KMP算法51Testing软件测试网;~_Aa { w]y)w
51Testing软件测试网aq8\;}s~ EH}
18.贪心51Testing软件测试网;CRe7K;F
*会议问题
f.M2n%w)oxE0(1) n个活动每个活动有一个开始时间和一个结束时间,任一时刻仅一项活动进行,求满足活动数最多的情况。
y$U g/z9C{ T0解:按每项活动的结束时间进行排序,排在前面的优先满足。
&n#V"U{)o&V051Testing软件测试网H TTKbel;o
(2)会议室空闲时间最少。51Testing软件测试网 G7n7dId"v+F'ko'x
51Testing软件测试网i$@)q-D([qf
(3)每个客户有一个愿付的租金,求最大利润。
.u4jUu^6@0
p5v j;Q)T8WhBW0(4)共R间会议室,第i个客户需使用i间会议室,费用相同,求最大利润。
?S*G c$z"`lhU051Testing软件测试网m"U6`a D&Q j
附录1 常用技巧
0m0v prg$v-YH01.带权中位数
C5C wt`r-@ Q0我国蒙古大草原上有N(N是不大于100的自然数)个牧民定居点P1(X1,Y1)、P2(X2,Y2)、 …Pn(Xn,Yn),相应地有关权重为Wi,现在要求你在大草原上找一点P(Xp,Yp),使P点到任 一点Pi的距离Di与Wi之积之和为最小。   51Testing软件测试网b0l\$~&Og1C
   即求 D=W1*D1+W2*D2+…+Wi*Di+…+Wn*Dn 有最小值   51Testing软件测试网 r wI6?'u!~ R
结论:对x与y两个方向分别求解带权中位数,转化为一维。
)K W4Xf4ur0设最佳点p为点k,则点k满足:51Testing软件测试网$N2UFI"F9E2eT
令W为点k到其余各点的带权距离之和,则
*d)icM?$M0sigema( i=1 to k-1) Wi*Di < = W/2
?hO{a#~+I_0sigema( i=k+1 to n) Wi*Di < = W/2
H@9~/n,v F LZ0同时满足上述两式的点k即为带权中位数。
\"L7uH)J'ln9Gu051Testing软件测试网w9FAP3? nD!fN X
2.求一序列中连续子序列的最大和51Testing软件测试网%PpQ;]I.Nw7qTL]
begin51Testing软件测试网 f,_jY:?4h tS-t}6F
maxsum:=-maxlongint;51Testing软件测试网v/`Hu%?8Er ~vY
sum:=0;51Testing软件测试网wy_V^|5z _
for i:=1 to n do begin
%v{ D4Ano!pn0inc(sum,data[i]);
n:v4X.{*p Q [0if sum >maxsum then maxsum:=sum;51Testing软件测试网"v)}$~1MU-tl
if sum< 0 then sum:=0;
;]}K/\JQ)NB ^)z V:oM0end;
J1r#yK2~Yt0writeln(maxsum);
unoc+m)Bv&rMm0end;51Testing软件测试网T5Ia+\h5@8S

TAG: Delphi编程

 

评分:0

我来说两句

Open Toolbar