打不死的心态活到老。

基本算法(用 PASCAL 描述)

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

51Testing软件测试网Tvx0[7o4z0F&f

转:http://www.cnblogs.com/dingjie/archive/2005/07/15/193216.html

}:\`2]D&l:ye051Testing软件测试网(SsYd!k A)lhS

基本算法(用 PASCAL 描述)51Testing软件测试网4l(u&c lVb-D

1.数论算法
M co b9T ];wlR0求两数的最大公约数51Testing软件测试网e i$@v(]2t:uc
function gcd(a,b:integer):integer;51Testing软件测试网YMsy;O8@M ~
begin51Testing软件测试网9G;S1l9wl4v-]
if b=0 then gcd:=a
e:[*ZO'P#q0else gcd:=gcd (b,a mod B);
+eJH(T.nn0end;51Testing软件测试网x g I}U`6~ h"M:P

$JT!zkm6~z0求两数的最小公倍数
[ t7F){xZZ3|@l0function lcm(a,b:integer):integer;51Testing软件测试网#icnsz
begin
/Ea gs3b3B0if a< b then swap(a,B);
)tNP weCV AY0lcm:=a;51Testing软件测试网,L\+C ah
while lcm mod b >0 do inc(lcm,a);51Testing软件测试网4j"`_;|J OQ
end;51Testing软件测试网tUV nS
51Testing软件测试网3am@%_*[/t OY
素数的求法
@ }5X3Tl:c0A.小范围内判断一个数是否为质数:
!_,?h i9vB}0function prime (n: integer): Boolean;51Testing软件测试网:S#U i!G(i:]I-I l
var I: integer;
4ce J1[+\0begin51Testing软件测试网IvyO8m(Mq2B
for I:=2 to trunc(sqrt(n)) do
c#FN~;R2G]4k0if n mod I=0 then begin51Testing软件测试网'Cj6j,M%D7J b
prime:=false; exit;51Testing软件测试网"s Vnyq5_+LN v
end;
%R _j+k:Z ibCh2O9{0prime:=true;51Testing软件测试网!F5bA8@u
end;
6s#V'zl Im?051Testing软件测试网T7t)_ HV[P&_V
B.判断longint范围内的数是否为素数(包含求50000以内的素数表):51Testing软件测试网*|d J'H ss
procedure getprime;
:s)IOBR'S0var
C:`Z8NM:QE%BN0i,j:longint;51Testing软件测试网n*g+ztV,qI
p:array[1..50000] of boolean;51Testing软件测试网-LS xS$K\
begin
}6C9Bm%oR!mD4Y#l0fillchar(p,sizeof(p),true);51Testing软件测试网A a^ l(MM)T1P {$|-d
p[1]:=false;51Testing软件测试网M^/JNc ^}
i:=2;
)M vJ:T'W6K4_U0while i< 50000 do begin51Testing软件测试网Gm,xWn/Ad
if p[i] then begin51Testing软件测试网v!m#IpjuoI*T
j:=i*2;51Testing软件测试网n8bt3EzHF2tw
while j< 50000 do begin51Testing软件测试网:Mne8M#x{y
p[j]:=false;51Testing软件测试网}(Of RkuY.q
inc(j,i);51Testing软件测试网JJ;M)~/x ]e
end;
`$@d ETp)e3x0end;51Testing软件测试网nlC%~ gk
inc(i);51Testing软件测试网z3y/?*iE#jN
end;51Testing软件测试网$\,N3odAwt e [[P,Y
l:=0;
1i(v6x$[ }/D0w(EW0for i:=1 to 50000 do
;KdGLU0if p[i] then begin
'g.U;|*{~$_9hV h0inc(l);pr[l]:=i;51Testing软件测试网R`U Da
end;51Testing软件测试网 c6]YY7]rd
end;{getprime}
!Bmg8gje0
C1a6V/K;[/Z{ }9lu0function prime(x:longint):integer;
Z7s%O/jI y!r(O0var i:integer;
&B;j!Q@ oegin51Testing软件测试网0T-}#p ~,@&od
prime:=false;
K2T,G6Wm;r0for i:=1 to l do
N7_/j"e"t4m&Ta0if pr[i] >=x then break51Testing软件测试网R)v"P.u0y{4]a1?4z!F
else if x mod pr[i]=0 then exit;51Testing软件测试网K,c;r7s,u:c ^0i!j
prime:=true;
y&EO$d3iF\\0end;{prime}
5b9`"|1bia0
'd FRj%J+\02.51Testing软件测试网Z-[S9u1~dB#w#a

+`2c[yN {"w A:@03.
.q9]r\v7s_051Testing软件测试网u l G9~ecs
51Testing软件测试网6J`@4WQ9`Mr~.\0W G
4.求最小生成树
j%Z&^6inq n0A.Prim算法:51Testing软件测试网EZ0c|#L9[T.{^m
procedure prim(v0:integer);
%cf bW9yx;G7l.}0var
-Kt[y/G$M`0lowcost,closest:array[1..maxn] of integer;51Testing软件测试网] XL]ez[#U$f
i,j,k,min:integer;51Testing软件测试网;?/amYR Jxg{Q
begin
h!\0}@"O+P)W&RM,t0for i:=1 to n do begin
,xD0Z Vvz0lowcost[i]:=cost[v0,i];
%B,qw9\4V ge3d3^0closest[i]:=v0;51Testing软件测试网e$?:@? T~(YI
end;
$NG VW S8k8N0for i:=1 to n-1 do begin
\Xo"^k^Ao&j0{寻找离生成树最近的未加入顶点k}
c\@2~ O@A"x0min:=maxlongint;
fjz m1u2lG0for j:=1 to n do51Testing软件测试网l K6O4sLsL%Q
if (lowcost[j]< min) and (lowcost[j]< >0) then begin
.I%]7I_3e H,^0min:=lowcost[j];
Dp/J]4~2lZ*Q0k:=j;51Testing软件测试网 Z3`?*l#|^L
end;51Testing软件测试网}#r+b6NI|
lowcost[k]:=0; {将顶点k加入生成树}51Testing软件测试网KOl)Ff9H/uN8yd1d%K
{生成树中增加一条新的边k到closest[k]}51Testing软件测试网4Fn@0^%uU
{修正各点的lowcost和closest值}
A_~Z8gvf1?0for j:=1 to n do51Testing软件测试网 VBH$?"j,m3w
if cost[k,j]< lwocost[j] then begin
W c:S ^4e@zQy0lowcost[j]:=cost[k,j];51Testing软件测试网Xu/vb`+o r$IU5Dp {
closest[j]:=k;
k8C3T@%E6vD0end;
c7Z8B#W Ri"T Z0end;
g8v9aHF0end;{prim}51Testing软件测试网!Qe'd\5X"C!~OT

|dZ Po!JvV0B.Kruskal算法:(贪心)
0i!^j n;P0按权值递增顺序删去图中的边,若不形成回路则将此边加入最小生成树。51Testing软件测试网C`+X\sEp
function find(v:integer):integer; {返回顶点v所在的集合}51Testing软件测试网ZOv/V umZ
var i:integer;
@D+R(arZn0begin51Testing软件测试网 bI+F}$V h%W
i:=1;
-|HGU'qOV|l ]{0while (i< =n) and (not v in vset[i]) do inc(i);
k j x8tI@,V5\0if i< =n then find:=i else find:=0;
q lVY?S I0end;51Testing软件测试网*[5E3o"cr1m
51Testing软件测试网*S-^J8S6XP
procedure kruskal;
9F9k|7v"jM}k T8^znM0var
8K-Rpi;]z5e0tot,i,j:integer;
"F,Z3o|G\T`8~1{Z0begin
O-}Z-txnf$aj.V1B RY0for i:=1 to n do vset[i]:=[i];{初始化定义n个集合,第I个集合包含一个元素I}
V TUm7Q I,t0p:=n-1; q:=1; tot:=0; {p为尚待加入的边数,q为边集指针}51Testing软件测试网'`4f \HZ1K.cs?g
sort;51Testing软件测试网oP?ha&Rt
{对所有边按权值递增排序,存于e[I]中,e[I].v1与e[I].v2为边I所连接的两个顶点的序号,e[I].len为第I条边的长度}
a7n'n8H%r*ke8O7YG0while p >0 do begin
4M4aYB9~"M&v"gD#R0i:=find(e[q].v1);j:=find(e[q].v2);51Testing软件测试网(tJ4BP.{eae^9q
if i< >j then begin
se9WUIp,U0inc(tot,e[q].len);51Testing软件测试网B\x$@y o
vset[i]:=vset[i]+vset[j];vset[j]:=[];
&a*bv6Z$P3}b;}6C0dec(p);
/C,g-@mLCN0end;51Testing软件测试网F.H y4Atj/`UmG
inc(q);51Testing软件测试网$@@v0BS4B8B:~&h
end;
|@q[ ^UNg W0J0writeln(tot);
:c)a-@8yS2H b,dE0end;
6TD.n/q,Ws5V e051Testing软件测试网 a)f"U-KxLQ

RZs1W0Y(D05.最短路径
7GbW}+b8D]0A.标号法求解单源点最短路径:51Testing软件测试网^ Z} m8h"D^
var
~BsOvN0a:array[1..maxn,1..maxn] of integer;51Testing软件测试网)MQrc9SM.W
b:array[1..maxn] of integer; {b[i]指顶点i到源点的最短路径}
[)ONj3I3aQm0mark:array[1..maxn] of boolean;
4j'G&^k&C[:q051Testing软件测试网w;O lj lXG*`7Y1a
procedure bhf;
\ [bU!JB uB(e9]oRK0var
O3eL._"V(Zr:M0^D0best,best_j:integer;
0de$Dyf0begin
@*j lI3`0fillchar(mark,sizeof(mark),false);
4B%A;J2\#z[1IKd-G0mark[1]:=true; b[1]:=0;{1为源点}
Zfs!h t1K/N0repeat51Testing软件测试网 R^_q\'WWD s
best:=0;
fK,n1@_0for i:=1 to n do
cI!^6[,J}?H6^H0If mark[i] then {对每一个已计算出最短路径的点}51Testing软件测试网k_0n C w+f fX*KB$?:U5e
for j:=1 to n do
9\7F|z%a$vv_0if (not mark[j]) and (a[i,j] >0) then51Testing软件测试网 n3urP-{*`
if (best=0) or (b[i]+a[i,j]< best) then begin51Testing软件测试网)qVqN3T$xf*P O
best:=b[i]+a[i,j]; best_j:=j;51Testing软件测试网ve-o_4Z{0@)b
end;
?T5c$hB(E0if best >0 then begin
?dG+G T0b[best_j]:=best;mark[best_j]:=true;51Testing软件测试网[$P8iq Q'[}`q
end;51Testing软件测试网w4v!atD
until best=0;
(bVMM fT C5~m0end;{bhf}
b)H E3z.Kd H0CKM1aM0
[E-r@s0tML9\ D0B.Floyed算法求解所有顶点对之间的最短路径:
$Ity Vt(if-V0procedure floyed;
0tS)u;}^,^w0begin51Testing软件测试网Ugy-mD{Ru!N
for I:=1 to n do51Testing软件测试网?&rQ5g0k~
for j:=1 to n do
;t$a:e\f!{0if a[I,j] >0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]表示I到j的最短路径上j的前驱结点}
.P1V"s:j l^0Cx6V0for k:=1 to n do {枚举中间结点}51Testing软件测试网r&u-m~\4l,["c;d,`
for i:=1 to n do
0z"tX,H7` ]0for j:=1 to n do51Testing软件测试网$LnB7hm Ma
if a[i,k]+a[j,k]< a[i,j] then begin51Testing软件测试网?Q/K\ kJ;v
a[i,j]:=a[i,k]+a[k,j];51Testing软件测试网3n ?}g5W@&q%u
p[I,j]:=p[k,j];
*D D vG Xj0end;
IujO |`0end;
Z:ri&n7Vx hZ#`051Testing软件测试网-N D4{cej
C. Dijkstra 算法:
}5Y"@^)G0类似标号法,本质为贪心算法。51Testing软件测试网V1@X {,sWM
var
$X)bMc;MoYs%m0a:array[1..maxn,1..maxn] of integer;51Testing软件测试网NF\9Rv
b,pre:array[1..maxn] of integer; {pre[i]指最短路径上I的前驱结点}
"jZM0}$\0mark:array[1..maxn] of boolean;51Testing软件测试网 G"Ha6s.G f+Q
procedure dijkstra(v0:integer);
%xp!U Q&Cu,bf)b(H0begin
DC:ekJ4_7I&q0fillchar(mark,sizeof(mark),false);51Testing软件测试网"gWHe5r*MS(]
for i:=1 to n do begin51Testing软件测试网6s6Ev[Nr7}#T:bg
d[i]:=a[v0,i];
tlV0u r0if d[i]< >0 then pre[i]:=v0 else pre[i]:=0;51Testing软件测试网%A(f ]x VA$F
end;51Testing软件测试网/F7VV V0Hdv
mark[v0]:=true;
0U4@` }'HZ0o0repeat {每循环一次加入一个离1集合最近的结点并调整其他结点的参数}51Testing软件测试网L"g8`1z}(k
min:=maxint; u:=0; {u记录离1集合最近的结点}51Testing软件测试网#X1H#NMg2{
for i:=1 to n do51Testing软件测试网,J.Qn]ru)r&q)I7C
if (not mark[i]) and (d[i]< min) then begin51Testing软件测试网$U(~ h2F ~;v
u:=i; min:=d[i];51Testing软件测试网+U*iT;X(xX#bv1E
end;51Testing软件测试网{#U:jb^ ?4YDj;i
if u< >0 then begin51Testing软件测试网y ~&|-S?{k
mark[u]:=true;
#~ LC#w.i)M u^r.n0for i:=1 to n do51Testing软件测试网t?8F|(hX1t4r
if (not mark[i]) and (a[u,i]+d[u]< d[i]) then begin
~"d_2N6s:nH Yx I0d[i]:=a[u,i]+d[u];51Testing软件测试网6[ Iu7`"d
pre[i]:=u;51Testing软件测试网o&V mj1s@;w'D x}3k
end;51Testing软件测试网 F8qHQedJQ+s
end;
Q%vE` s mS+M0until u=0;
*{J4Y7v7E0end;51Testing软件测试网#^j%TH e
51Testing软件测试网 ?&L3z9W+k)}w
D.计算图的传递闭包
Buuw^&Q"?5`0Procedure Longlink;51Testing软件测试网-\];Ss*v Rz
Var51Testing软件测试网%aud8aw#r J9@d%c7~\6v
T:array[1..maxn,1..maxn] of boolean;
0og1JL|\.[0Begin51Testing软件测试网7g&g rd3Q3gV
Fillchar(t,sizeof(t),false);
/L(l0z8Zm%O%mJ*u0For k:=1 to n do51Testing软件测试网q P@:|(~5CK'P(x
For I:=1 to n do
z*tC3i\8@m1j"C0For j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);51Testing软件测试网 r'qbU9uY7d
End;51Testing软件测试网:N8C;[8d!c5]lD
51Testing软件测试网D:Z.Ax`[7{

D EY?h"C7W'g.x06.0-1背包问题(部分背包问题可有贪心法求解:计算Pi/Wi)
~S*v b{N0数据结构:
cdc2kZt d0Y0w[i]:第i个背包的重量;51Testing软件测试网T!Nt` D0{ Y+p
p[i]:第i个背包的价值;51Testing软件测试网Qb@8oL2z
(1)0-1背包: 每个背包只能使用一次或有限次(可转化为一次):
%K? h%a(E7C(D0A.求最多可放入的重量。51Testing软件测试网[2OoGQ/cp+q
NOIP2001 装箱问题51Testing软件测试网~7WP5^2[I`b
有一个箱子容量为v(正整数,o≤v≤20000),同时有n个物品(o≤n≤30),每个物品有一个体积 (正整数)。要求从 n 个物品中,任取若千个装入箱内,使箱子的剩余空间为最小。
`D{^.Gx2IIB_"QQ0l 搜索方法51Testing软件测试网?7b^i1I9Mg@
procedure search(k,v:integer); {搜索第k个物品,剩余空间为v}
'h&FMa-h.L0var i,j:integer;
]k'ik f(rDo0begin51Testing软件测试网n(lz3M&kBZtc
if v< best then best:=v;51Testing软件测试网B B VN{k
if v-(s[n]-s[k-1]) >=best then exit; {s[n]为前n个物品的重量和}51Testing软件测试网:_%|\T%t-I9D,UEx
if k< =n then begin51Testing软件测试网n9h0s^9}
if v >w[k] then search(k+1,v-w[k]);
vxx&C-s;^iH[0search(k+1,v);51Testing软件测试网6uIoLZ/ca?
end;
yR.KW"j7Y2B-U0end;
;Y#u.`[9Db:k&?0
:d:H$C4k)kUfJ(p0l DP
-_v'i,q8D`8CJ0F[I,j]为前i个物品中选择若干个放入使其体积正好为j的标志,为布尔型。51Testing软件测试网*J {8x_0Y ?!i
实现:将最优化问题转化为判定性问题51Testing软件测试网 ]m-Pyg Lu
F[I,j]=f[i-1,j-w[i]] (w[I]< =j< =v) 边界:f[0,0]:=true.
Ly-y Bt[[0For I:=1 to n do
v;J%sV4Q5J0For j:=w[I] to v do F[I,j]:=f[I-1,j-w[I]];51Testing软件测试网1C7`!xQ/Y}L
优化:当前状态只与前一阶段状态有关,可降至一维。
GW];oG!_0F[0]:=true;
R.aE4Z#@l)yp0For I:=1 to n do begin51Testing软件测试网.sG x3BH`
F1:=f;
]a$A#sRz0For j:=w[I] to v do
WCWDT0If f[j-w[I]] then f1[j]:=true;51Testing软件测试网E Gt$n L5Y6Az g
F:=f1;
a?v[^(n,oYq0End;51Testing软件测试网qJ W8bFn(ivL
51Testing软件测试网Z)U-v8r4]o'|
B.求可以放入的最大价值。51Testing软件测试网Q+FH4]2N?;Ugc/s
F[I,j]=
y~.FU _051Testing软件测试网7o]kL[

[ P B-o3yx:i.z0C.求恰好装满的情况数。51Testing软件测试网lm8h,uqDTa6Dh

/wt+t{!^7RJ#MG#~0 51Testing软件测试网Qt"cuR7[d,nE

#l1XK8IF&CV0(2)每个背包可使用任意次:
1e,xFP P0A.求最多可放入的重量。
ssqFY fq/?!z0状态转移方程为51Testing软件测试网9e'\%{ [w+O;?+_|f1vb
f[I,j]=max{f[i-w[j]
8U#H5z$Z^{Q*E051Testing软件测试网$sJt-s'Fv r3EVg
 
Kb)J2\?|hV\ p1]0
!f)J"MkcSC0
0w;h6]!J5G@G4B qi!f:c0B.求可以放入的最大价值。
[t!s:h.Uc:\%K0USACO 1.2 Score Inflation51Testing软件测试网R8~"{$oo
进行一次竞赛,总时间T固定,有若干种可选择的题目,每种题目可选入的数量不限,每种题目有一个ti(解答此题所需的时间)和一个si(解答此题所得的分数),现要选择若干题目,使解这些题的总时间在T以内的前提下,所得的总分最大,求最大的得分。51Testing软件测试网'NcLO NZ `9W/vCp
*易想到:51Testing软件测试网T2MlOC#|_
f[i,j] = max { f [i- k*w[j], j-1] + k*v[j] } (0< =k< = i div w[j])
,D{ pu(q1@0其中f[i,j]表示容量为i时取前j种背包所能达到的最大值。51Testing软件测试网BN8r:Vsw'h
*优化:51Testing软件测试网7u+hB-K$Pt/L"w0|br$U
Begin
)YQlYK5uU8DM0FillChar(problem,SizeOf(problem),0);51Testing软件测试网$KtQC$e)ff%h(v
Assign(Input,'inflate.in');51Testing软件测试网Bj5G8b ?+E
Reset(Input);51Testing软件测试网.jk)O2z_'R'i
Readln(M,N);51Testing软件测试网?)Y?0xXr la
For i:=1 To N Do51Testing软件测试网J mlzXY/Wn
With problem[i] Do
M``mZ%J/R'u3Y(O0Readln(point,time);51Testing软件测试网s2c {"lh F4F"O W
Close(Input);
1|/J.c!O9Rm051Testing软件测试网 p ^@.OVW"N5CN
FillChar(f,SizeOf(f),0);
,}:l&[e"o0For i:=1 To M Do51Testing软件测试网5}%jY@j$c k @Q
For j:=1 To N Do51Testing软件测试网MTbh"vK!L"m"g
If i-problem[j].time >=0 Then51Testing软件测试网-oM#C.@c9R%e
Begin51Testing软件测试网3c3\z%^7g([V G
t:=problem[j].point+f[i-problem[j].time];51Testing软件测试网){l-kel X!^n8d
If t >f[i] Then f[i]:=t;
E'?+dP|"P_;J0a0End;
f%y!_;n {3OY1E051Testing软件测试网`i'Ie ee Q
Assign(Output,'inflate.out');
bI)Do ^AE0Rewrite(Output);
VJu%~a3z%r0Writeln(f[M]);51Testing软件测试网cc4YfTD B[
Close(Output);
-oo.u1E9m0End.51Testing软件测试网T&U9drhB2v
C.求恰好装满的情况数。51Testing软件测试网-h#? n U{,|h^a)S;a6T
Ahoi2001 Problem2
`-g0{G+`D4R+oz]t0求自然数n本质不同的质数和的表达式的数目。
2BUK3R })px ?0思路一,生成每个质数的系数的排列,在一一测试,这是通法。
G@$l,Yc)sS r0procedure try(dep:integer);
M6XU6P iT0var i,j:integer;
~P,Qf7vr0begin51Testing软件测试网T%eIQ9W!i(lQ Y
cal; {此过程计算当前系数的计算结果,now为结果}
J.AP8k"h[E0if now >n then exit; {剪枝}51Testing软件测试网eV(c$c_o&OS;f
if dep=l+1 then begin {生成所有系数}
;E9bn)Ojk4fW0cal;
r}1V|\YP:v0if now=n then inc(tot);
't\zX/[t4eK6S!Q0exit;
@2v1Sz)sV| q2Q{&B0end;51Testing软件测试网jG0rWZv[)t
for i:=0 to n div pr[dep] do begin
s%KfqP.y0xs[dep]:=i;
&E nd"F$Fd0try(dep+1);
f{r` ^ J2Q7X&I0xs[dep]:=0;
t0nz5`&k(?0end;51Testing软件测试网"cD0TC+T$|8I;J3Nt Gs
end;51Testing软件测试网 zC}^C%J4}Dw(L
51Testing软件测试网(G9CfjS
思路二,递归搜索效率较高
^` h q W:o0gz0procedure try(dep,rest:integer);51Testing软件测试网1Z sgztGC
var i,j,x:integer;51Testing软件测试网`$o gf sZ
begin
,_0\1~0h.ZYTR0if (rest< =0) or (dep=l+1) then begin51Testing软件测试网sT} V-m,aX%]
if rest=0 then inc(tot);51Testing软件测试网/qy)bY'j
exit;51Testing软件测试网oc1K}x+j
end;51Testing软件测试网UF3D/j |k
for i:=0 to rest div pr[dep] do51Testing软件测试网7z2O+_ Hg^/Dp j
try(dep+1,rest-pr[dep]*i);
;X7xs v mc0end;51Testing软件测试网2LZM%fbZ*d }x? s
51Testing软件测试网3~5J^k/VI;d
思路三:可使用动态规划求解
N#Xq,Zmf0x'Y0USACO1.2 money system
cZo$E-`t)w:TD"xH{0V个物品,背包容量为n,求放法总数。51Testing软件测试网0X [/QS q+qA
转移方程:51Testing软件测试网FP4SA)^2wQG8\R
51Testing软件测试网lc.j8_8`
Procedure update;
!ldR4{X z)G h0var j,k:integer;51Testing软件测试网E Y;\5Yqh&I
begin51Testing软件测试网N8R\s6n.fWk"@
c:=a;51Testing软件测试网`,MG'~8@&fQh)`
for j:=0 to n do
2D|B%UK l5`,t0if a[j] >0 then
F&QwtT| J'L#D0for k:=1 to n div now do51Testing软件测试网-h4W4^l\6Q i8R&h'pW6w
if j+now*k< =n then inc(c[j+now*k],a[j]);
&~Us^Cyn3Z"b0a:=c;
"Jja4V!c}0end;
9rG(H8t `$M f0{main}51Testing软件测试网&[O\SrOBk(P$f
begin
G2O'|E U0read(now); {读入第一个物品的重量}
bw6e @z w?0i:=0; {a[i]为背包容量为i时的放法总数}
8P*[+p/l\R0while i< =n do begin51Testing软件测试网ja]1H:x1g
a[i]:=1; inc(i,now); end; {定义第一个物品重的整数倍的重量a值为1,作为初值}
D:A }9i$u%I0for i:=2 to v do
2q0A&q*L |;YyR0begin51Testing软件测试网hjDXi
read(now);51Testing软件测试网)v;g6Gc1j]
update; {动态更新}51Testing软件测试网i@4a/E*Habi
end;51Testing软件测试网\ } Q,e7J7K!p`
writeln(a[n]);51Testing软件测试网qpbML!E?

Pb Or3B$y+ka07.排序算法51Testing软件测试网:pxw0]S&F|
A.快速排序:
1Z n&z A(ZA7t}0procedure sort(l,r:integer);51Testing软件测试网B;m7}tnYv
var i,j,mid:integer;51Testing软件测试网V,CT A:pN
begin51Testing软件测试网*Y~rM3l-q
i:=l;j:=r; mid:=a[(l+r) div 2]; {将当前序列在中间位置的数定义为中间数}
NXZU3v,g[n7mW0repeat
s:_0@{(@0while a[i]< mid do inc(i); {在左半部分寻找比中间数大的数}51Testing软件测试网 U? ei7a*C*u?
while mid< a[j] do dec(j);{在右半部分寻找比中间数小的数}51Testing软件测试网/Ge1Vn!mEo},Wo
if i< =j then begin {若找到一组与排序目标不一致的数对则交换它们}51Testing软件测试网*vt V9]K;vs$p
swap(a[i],a[j]);51Testing软件测试网`Y-qK qOx/h`
inc(i);dec(j); {继续找}
][1t1V5s's$y_*y0t L0end;
!t{Q6LI;r,sN0until i >j;
r6E#c5D5{)W%HY.u0if l< j then sort(l,j); {若未到两个数的边界,则递归搜索左右区间}51Testing软件测试网z;e(} ^N
if i< r then sort(i,r);
j o\6liITu0end;{sort}51Testing软件测试网w4t;N1v*A5|(m
 
j iTa!`8K4@$P0
p8j*b#I/WB#T@0B.插入排序:
Y"UC(q p2l qBg#Lv0procedure insert_sort(k,m:word); {k为当前要插入的数,m为插入位置的指针}
Dy,C:w1Fvu*|0var i:word; p:0..1;
U`4j8Z(_4g0begin
6e`)hz.tKi&_ K0p:=0;51Testing软件测试网v }'gDj3C{
for i:=m downto 1 do
/?E+b2r jo0if k=a[i] then exit;51Testing软件测试网 Xe#w4w%V*p1_
repeat
J-{3IE1B"~W:@0If k >a[m] then begin51Testing软件测试网cjktG
a[m+1]:=k; p:=1;51Testing软件测试网+W Tio/]
end51Testing软件测试网;g ^E]1a"tt*H+n
else begin51Testing软件测试网t/L K(u i8b
a[m+1]:=a[m]; dec(m);
7~i1w8uv]&aC0end;51Testing软件测试网*^J\5ROn
until p=1;
x5N}p#@'i0end;{insert_sort}51Testing软件测试网L O+b,|3XE
l 主程序中为:
3\,QYl#_Q0a[0]:=0;51Testing软件测试网l}b:O}v$EBBO
for I:=1 to n do insert_sort(b[I],I-1);51Testing软件测试网g-yaM?a
51Testing软件测试网%coO`?J9S m#F3nL
C.选择排序:51Testing软件测试网5snB'O6_,Y5R
procedure sort;51Testing软件测试网#]/I']jEm
var i,j,k:integer;
1c8T(l wL$_E0begin
]^nFjl!I)oy0for i:=1 to n-1 do begin51Testing软件测试网 T2{4D!UK:Kq3g
k:=i;
y-mGLrK`,U.B0for j:=i+1 to n do51Testing软件测试网M`c}x
if a[j]< a[k] then k:=j; {找出a[I]..a[n]中最小的数与a[I]作交换}51Testing软件测试网 W&GF&}P|m1e
if k< >i then begin
4Nl6[^#N0jL)k0a[0]:=a[k];a[k]:=a[i];a[i]:=a[0];
$X| C!q%ivdR;Q0end;51Testing软件测试网5aQhOV$b.\(b!f
end;
)m$r ns.DS{h0end;
&s p7S_/bSg |!`0
:T$C~@7F"f;H0D. 冒泡排序
Z_F9@&S g Ho0procedure sort;
-Kux?` [&K0var i,j,k:integer;51Testing软件测试网7K6a5v$q Kf:O
begin51Testing软件测试网Bb&I(a^r7`|
for i:=n downto 1 do51Testing软件测试网 r/A(E&GC%~u y G8L*eN
for j:=1 to i-1 do
0l xH!Yp$\0if a[j] >a[i] then begin51Testing软件测试网$_k m,vk:n2]
a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];
b \H C;Z4n5XqL:x5r0end;
?~2Cj5b2G!cL0end;51Testing软件测试网/Z7j1Y7v n

i N"}?8T.SlJ0E.堆排序:
}7B,@u(n j;XR0procedure sift(i,m:integer);{调整以i为根的子树成为堆,m为结点总数}51Testing软件测试网EvUIwP[
var k:integer;51Testing软件测试网C4x8bXB _l,c&hT
begin51Testing软件测试网3p Rc4q,Tx?6|k
a[0]:=a[i]; k:=2*i;{在完全二叉树中结点i的左孩子为2*i,右孩子为2*i+1}51Testing软件测试网%p2Rh CqX#B+l(X
while k< =m do begin
x B:t%m{q+@0if (k< m) and (a[k]< a[k+1]) then inc(k);{找出a[k]与a[k+1]中较大值}51Testing软件测试网3j0H3V6\e9xpdd6E
if a[0]< a[k] then begin a[i]:=a[k];i:=k;k:=2*i; end
+Iz&m'x$s5{;E0else k:=m+1;51Testing软件测试网&S:z G z$OPm*`w
end;
EE0gV7D/^b0H[0a[i]:=a[0]; {将根放在合适的位置}
`}(uv|@,F3V J)[(@0end;
;W:G h RQ FK0A0
(MmV8O:|D"n|N0procedure heapsort;
H$@4bMa-e*c6O0var
&d[7[9~ {0j:integer;
Z+b S7] P&I0begin51Testing软件测试网m2D#c*e$A7]0z3dG*~
for j:=n div 2 downto 1 do sift(j,n);51Testing软件测试网(P:mk^I ~'rC x
for j:=n downto 2 do begin
)e!lo5ne5OU$m0swap(a[1],a[j]);
pE"?*oa3wn0sift(1,j-1);
3C7c:S*XH5o0end;
UXVmN6Y'~0end;
)CMQc2QQFQ'`x.k\0
jI-mZ7s@:J)PNo0F. 归并排序51Testing软件测试网M `AS x9S4WN8k3s3C
{a为序列表,tmp为辅助数组}51Testing软件测试网9I weRu:X6{bk
procedure merge(var a:listtype; p,q,r:integer);
4Tr:?^s3g8_0{将已排序好的子序列a[p..q]与a[q+1..r]合并为有序的tmp[p..r]}51Testing软件测试网_Z I3gIV&_4I5h)l8XS
var I,j,t:integer;51Testing软件测试网$C%u Q!}6}\.zJ"_
tmp:listtype;51Testing软件测试网.Q{z Au%F&q ?
begin51Testing软件测试网Z3j9j q1]
t:=p;i:=p;j:=q+1;{t为tmp指针,I,j分别为左右子序列的指针}
.@t \R.PS0while (t< =r) do begin
H,j)M(kL_ f ^0if (i< =q){左序列有剩余} and ((j >r) or (a[i]< =a[j])) {满足取左边序列当前元素的要求}51Testing软件测试网^\1]*ck4^S
then begin51Testing软件测试网5X&TKVQ`/h&_(~
tmp[t]:=a[i]; inc(i);51Testing软件测试网8T2|I F(y.y,t
end51Testing软件测试网,h Odoz9O'[R#A
else begin
-@6WKpy h \r0tmp[t]:=a[j];inc(j);51Testing软件测试网sD(V2k;p
end;51Testing软件测试网^D k`gq)T"u-L
inc(t);
.T+Z2k6sN$U'u(fg2W,g0end;51Testing软件测试网XEm5n)@DN
for i:=p to r do a[i]:=tmp[i];51Testing软件测试网 n$M?8^6M
end;{merge}51Testing软件测试网oC"~PtgZ5X-o

F'e*v0n3L5L0procedure merge_sort(var a:listtype; p,r: integer); {合并排序a[p..r]}
RM)n1a!wn0var q:integer;51Testing软件测试网M|!b/s I.M]
begin51Testing软件测试网u[!D1\Z3K[%E
if p< >r then begin
?5Ak8A2X3F@+M0q:=(p+r-1) div 2;
DK&cT&|`"\ i0merge_sort (a,p,q);
D&G:H HjB b0merge_sort (a,q+1,r);
:O]Vw)R}\0merge (a,p,q,r);51Testing软件测试网N3Y%Z7_j(]-`$z.T9C{
end;
e p X bi0end;51Testing软件测试网,a4q*AYI/c
{main}51Testing软件测试网$J/M;?FC
begin
!i1k5[Rz'k @T.D0merge_sort(a,1,n);51Testing软件测试网J9E5t4i&O@6_?N\
end.51Testing软件测试网R*bNI-r#GH.b A1N
51Testing软件测试网v,v3@V^-O0bR

r+d%n1k9p~0V0G.基数排序
Oy3J(hnAn0思想:对每个元素按从低位到高位对每一位进行一次排序
P o k3n,mh0B:La p-vQ051Testing软件测试网 r(`0Tq,D!B8qV

(`&P~9t#Z8?K3A08.高精度计算
b8`3r` c;k,t(l6Xl7e0A.51Testing软件测试网5I E/h]0UW J
B.
G&Pg WYv%h0C.
.zYp_#K,B0D.51Testing软件测试网8n.dp#RH)K)` QA n
51Testing软件测试网3n9x"{EKT
9.树的遍历顺序转换
n^5h"H'Y%J!D0A. 已知前序中序求后序51Testing软件测试网(OM+l2g'L
procedure Solve(pre,mid:string);
t U-i:gl'~0var i:integer;
&UU$x@4Vb"dyf0begin
P.ll~s!d/^\0if (pre='') or (mid='') then exit;
-F6]4C!bvq0i:=pos(pre[1],mid);
7KG [O+qk6@5L,aiX0solve(copy(pre,2,i),copy(mid,1,i-1));51Testing软件测试网d/d-_iYCg
solve(copy(pre,i+1,length(pre)-i),copy(mid,i+1,length(mid)-i));
#BL(@^6z+| HQU0post:=post+pre[1]; {加上根,递归结束后post即为后序遍历}51Testing软件测试网+A TV-?4FA
end;
/]`m"Pm7OZ0
O1TL~ jdk sL0B.已知中序后序求前序51Testing软件测试网7jm Pb6iTb j
procedure Solve(mid,post:string);51Testing软件测试网4W"@I$ln*[?NKl
var i:integer;51Testing软件测试网%[G2Ogt qi6WE
begin
$p6~-qke\0if (mid='') or (post='') then exit;51Testing软件测试网#Q1N(cB~E'Y]YO
i:=pos(post[length(post)],mid);51Testing软件测试网)?`!\2A(B}
pre:=pre+post[length(post)]; {加上根,递归结束后pre即为前序遍历}51Testing软件测试网(v5e,LW)\}k8f
solve(copy(mid,1,I-1),copy(post,1,I-1));
P:D n8~1I0solve(copy(mid,I+1,length(mid)-I),copy(post,I,length(post)-i));
!Z]NxW0end;
5F*C&f;xp q051Testing软件测试网8^xM3AB#Q3Zaeb
C.已知前序后序求中序51Testing软件测试网l#b1s1if8@

%F0iS$v+GU0function ok(s1,s2:string):boolean;51Testing软件测试网`/T5e&]k3a
var i,l:integer; p:boolean;
/r3e%LyU9K+e:B7k8H0begin51Testing软件测试网|6[3]Z0^ dRw
ok:=true;
t8Wq:J(d9~2Fp0l:=length(s1);
+h%f:z2j%B:K/| C0for i:=1 to l do begin
,`$na6rP8MG @:aD \5Y0p:=false;
pf4x*qyTa*S0for j:=1 to l do
9qf!w{ AB[K0if s1[i]=s2[j] then p:=true;
3_DL'l!nWD0if not p then begin ok:=false;exit;end;51Testing软件测试网F B9d2W7d3X
end;51Testing软件测试网kX_JS?Y
end;51Testing软件测试网,W%nGZ3K^R
51Testing软件测试网,xMkux+V4Vk
procedure solve(pre,post:string);51Testing软件测试网zR7e2A^v%wWD
var i:integer;51Testing软件测试网4C&^%]]+|
begin51Testing软件测试网 BO/zu^ `
if (pre='') or (post='') then exit;51Testing软件测试网4Pt8}mJ]
i:=0;
S#nW W7g#E3e0repeat51Testing软件测试网D/I~r ku-F
inc(i);51Testing软件测试网g"K8`2{ S0xb5M&~,Tr|
until ok(copy(pre,2,i),copy(post,1,i));
4In L^6jY0solve(copy(pre,2,i),copy(post,1,i));51Testing软件测试网W_n#i1EA
midstr:=midstr+pre[1];51Testing软件测试网jMH?V$s
solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1));51Testing软件测试网7}$QL}l0f
end;51Testing软件测试网+N1bNIsMIc
51Testing软件测试网`%Y;ie}/ok
10.求图的弱连通子图(DFS)
/tM'C]R:?+r$`5ib0procedure dfs ( now,color: integer);
3w&[R7t)]0begin
Z nj0wyp+}0for i:=1 to n do
+Y!tyX6^pR0if a[now,i] and c[i]=0 then begin51Testing软件测试网p:NSI5C^Z/n'J
c[i]:=color;
J2^0{:IB%]Lu0dfs(I,color);
Q8fPI/fl+`0end;
9E v3Lh'n F8I@(me0end;
YL!FG,[ n0
4P9F^ J2Z+F4G1zk051Testing软件测试网-{h+~ ^T%zk
11.拓扑排序
a:B]"` E#UK z0寻找一数列,其中任意连续p项之和为正,任意q 项之和为负,若不存在则输出NO.51Testing软件测试网 P I/U \/PM

e-T$@%O/QyqJ;hi9v0
|sw*SX$O)S Jm012.进制转换
hJ)r#M;B/]iJ0A.整数任意正整数进制间的互化
$f+V#w8nx]]#g4b9h0
6IZ*Ny+@&b0NOIP1996数制转换51Testing软件测试网)HFl A)G
设字符串A$的结构为: A$='mp'
%J6KI vB!Lj S0其中m为数字串(长度< =20),而n,p均为1或2位的数字串(其中所表达的内容在2-10之间)
Em aLzEkFX5m(@P0程序要求:从键盘上读入A$后(不用正确性检查),将A$中的数字串m(n进制)以p进制的形式输出.51Testing软件测试网](tq&\/xD
例如:A$='48< 10 >8'
0_#YW V TU/a0其意义为:将10进制数48,转换为8进制数输出.51Testing软件测试网t7K$j`T_K,en
输出结果:48< 10 >=60< 8 >
:`&HM6b?mb%H051Testing软件测试网ppUK,cW)\
B.实数任意正整数进制间的互化51Testing软件测试网[8o E@Y%NG:_/}"ufH
C.负数进制:51Testing软件测试网;m;z4xI2cw.Z
NOIP2000
-q#uf8me*t,b f+A0设计一个程序,读入一个十进制数的基数和一个负进制数的基数,并将此十进制数转换为此负 进制下的数:-R∈{-2,-3,-4,....-20}51Testing软件测试网.c*J#k2b M)c)y"u6~

#O{l2BZ'B9e013.全排列与组合的生成
4tX|+A LMye:D0排列的生成:(1..n)
6A)zi0e l0procedure solve(dep:integer);
(E#m/TmN+`(W!p0var
0pAzJu3I0i:integer;
!b_A}C;A4s7h0begin
oNBjt!Yr*j ~8]M0if dep=n+1 then begin writeln(s);exit; end;51Testing软件测试网[ _-yo!~fn/])y
for i:=1 to n do51Testing软件测试网KB4]9{MD%E7}*Nd5C
if not used[i] then begin51Testing软件测试网(j \;IpNL!{GD
s:=s+chr(i+ord('0'));used[i]:=true;51Testing软件测试网^PKL-LM
solve(dep+1);
qyuO X(_*E0s:=copy(s,1,length(s)-1); used[i]:=false;
t(P*T"b{_N0end;51Testing软件测试网%mx(lPW
end;
"ny)v1[hi\0G\0组合的生成(1..n中选取k个数的所有方案)51Testing软件测试网'kG)g}.QK\+CA
procedure solve(dep,pre:integer);
x&y9DZP%d)l Y0var51Testing软件测试网1E:nGwqi!I
i:integer;51Testing软件测试网R K"o} Ww
begin51Testing软件测试网uCy1Lx1Qj)BGV
if dep=k+1 then begin writeln(s);exit; end;
,aS\*xWz#[0for i:=1 to n do51Testing软件测试网,H4k d{-Oy(u&m5t k
if (not used[i]) and (i >pre) then begin
7w5U){na#Q0s:=s+chr(i+ord('0'));used[i]:=true;
i,R%m&pA T0solve(dep+1,i);
0y(F*Np;l4D0s:=copy(s,1,length(s)-1); used[i]:=false;51Testing软件测试网waJK(j;q
end;
5a1h&}}2@K,[8Z&|h0end;
0b@:fuM/?9yo0
O"p:{TJ4DwE0 
j2~Kyy hs051Testing软件测试网 t`\mB&W
14 递推关系
Nb8N-q~9nC|0计算字串序号模型
8UCv$sS;@d-W0USACO1.2.5 StringSobits51Testing软件测试网b*r*BxD
长度为N (N< =31)的01串中1的个数小于等于L的串组成的集合中找出按大小排序后的第I个01串。51Testing软件测试网l(Q1I|ps
51Testing软件测试网6aR!d y bd1J"s

Zq9p'r5Z.lH9gA0数字划分模型
/~&w/~fS.N5z5Q4b0*NOIP2001数的划分51Testing软件测试网:TzH(nLtd)R
将整数n分成k份,且每份不能为空,任意两种分法不能相同(不考虑顺序)。
yrbrlo9]&c0d[0,0]:=1;
_%nyZZch,v0for p:=1 to n do
6Ckg"K?MP0for i:=p to n do51Testing软件测试网D7d9a5]'b
for j:=k downto 1 do inc(d[i,j],d[i-p,j-1]);
z ~(M:Db!{~e0writeln(d[n,k]);51Testing软件测试网a)jS4Dr1q5K:w9k
51Testing软件测试网/L7j.Aaw/LV U
*变形1:考虑顺序51Testing软件测试网)v U'MjzU P
d[ i, j] : = d [ i-k, j-1] (k=1..i)
O [,}7ZH/_6xqW0*变形2:若分解出来的每个数均有一个上限m51Testing软件测试网;nQ,}:rI-w0q z
d[ i, j] : = d [ i-k, j-1] (k=1..m)
:i#v/z0DI4Q-Q0
5V3R _hk o`;}0
/?.h]1C P"|'l'm015.算符优先法求解表达式求值问题51Testing软件测试网L;zf8G}WO^
const maxn=50;51Testing软件测试网B4I,V0o ~AT}
var51Testing软件测试网,qVx"j(T y\G*r
s1:array[1..maxn] of integer; {s1为数字栈}
KxQ(}+uT-u0i0s2:array[1..maxn] of char; {s2为算符栈}51Testing软件测试网5Jhy}(c] S,G U
t1,t2:integer; {栈顶指针}
[Y n.ji0
(mw1Dg |Iu+B0procedure calcu;51Testing软件测试网#@[4@ N9|H
var51Testing软件测试网7^0U[ bwr s+g[
x1,x2,x:integer;51Testing软件测试网 a,i {I^
p:char;
7a+n)hS@2c0begin51Testing软件测试网)C+B(HQWL
p:=s2[t2]; dec(t2);
4Pn(H t ^0x2:=s1[t1]; dec(t1);
s _R8by n6V {0x1:=s1[t1]; dec(t1);51Testing软件测试网'J K.C3n3h
case p of
@l}Kq0'+':x:=x1+x2;
;t+]wHc#i0'-':x:=x1-x2;
0S"QOi H0'*':x:=x1*x2;
6`:h;kX#k*_0'/':x:=x1 div 2;51Testing软件测试网'\ pA[2~s9x
end;
"Y|a:@Gu0inc(t1);s1[t1]:=x;51Testing软件测试网X1qto0Z7t+^]
end;
C ^xcBe#x)\+r+iL0
5}7ZaB:OfQ6mm0procedure work;
&Uk|/k`:B*rO#x w0var c:char;v:integer;51Testing软件测试网)tefs+TE lx*Vn
begin51Testing软件测试网R!S6B V+Sy2H
t1:=0;t2:=0;51Testing软件测试网*Q&xpo&iXj
read&copy;;
5t&\A@ G0while c< >';' do
Y(p,i_7@5Ha+Z|7xW0case c of
0I'ut_*K"uc0'+','-': begin51Testing软件测试网DH u0J1CJ
while (t2 >0) and (s2[t2]< >'(') do calcu;
1L0?#Y1ds R,tR0inc(t2);s2[t2]:=c;
#ihs R-K8SNa8kjc D0read&copy;;
V2Yk2J\pp'D }m0end ;51Testing软件测试网},Ll6ZW;Hy?
'*','/':begin
sIcz,aZo0if (t2 >0) and ((s2[t2]='*') or (s2[t2]='/')) then calcu;
$_q{&W4y;E0inc(t2);s2[t2]:=c;
{-z~"XM I?B0read&copy;;51Testing软件测试网9p%H AXjf,F cAO2S
end;51Testing软件测试网v4Bu6@G
'(':begin inc(t2); s2[t2]:=c; read&copy;; end;51Testing软件测试网E u~0?^b4W-y^/[vV
')':begin51Testing软件测试网5y6RrP| |k,GR/H%k6E
while s2[t2]< >'(' do calcu;51Testing软件测试网!l1aJ e7Q3r
dec(t2); read&copy;;
!d'IEs0E,A:w)H6c0end;
s7v t&e;i w0'0'..'9':begin
/c*Fw!X/@&P;N&x0v:=0;
ldK(GLLn2EC!v0repeat
h z}3h8s3\0v:=10*v+ord&copy;-ord('0');
Es B`];tN[ F*B0read&copy;;
1YB\v't2w0until (c< '0') or (c >'9');
/J p\(` g#U9C7B:K!?0inc(t1); s1[t1]:=v;51Testing软件测试网^1cF H5L
end;51Testing软件测试网B/_ L$D9l c3t
end;51Testing软件测试网Z4q$?:~#k0r |3G
while t2 >0 do calcu;
_Sm(f/{e2}0writeln(s1[t1]);
s1tr"I Vi9O"N6lA\0end;51Testing软件测试网P1bUq,d7zA

Jr$nTt;_:Ne*d016.查找算法
A]&VOBC0折半查找51Testing软件测试网%RDuK@z^(B
function binsearch(k:keytype):integer;51Testing软件测试网;l0D2@:tD*F L
var low,hig,mid:integer;51Testing软件测试网:g}+yem5N|_.p
begin51Testing软件测试网aD(J6\+bNQn
low:=1;hig:=n;51Testing软件测试网;}(M(EH5jNZ
mid:=(low+hig) div 2;
+|]Y:f$q2kqeEF/z0while (a[mid].key< >k) and (low< =hig) do begin51Testing软件测试网~:h(P0?!q,q&d1p$B m
if a[mid].key >k then hig:=mid-1
8u \Y Q Vf0else low:=mid+1;51Testing软件测试网!W@T1_(x l$]\M
mid:=(low+hig) div 2;51Testing软件测试网 PE7aY/y
end;51Testing软件测试网J'y^6Wea O
if low >hig then mid:=0;
:hR L Q'hO0binsearch:=mid;
/\#Wgw6^']#Z\)z dX0end;51Testing软件测试网)Sz&[NGv,s H+k:v T Z
树形查找51Testing软件测试网a'{BOy7b9Lj(d
二叉排序树:每个结点的值都大于其左子树任一结点的值而小于其右子树任一结点的值。
2nxtP%c2^0查找
M9{g `E3S0function treesrh(k:keytype):pointer;51Testing软件测试网 _bE*_+K;y-^}+V
var q:pointer;
.E"|,de.t0begin51Testing软件测试网&^YD/C m,v
q:=root;
g!Yn koa)J(a/q;f0while (q< >nil) and (q^.key< >k) do51Testing软件测试网8a H(c6R Q3c$Q
if k< q^.key then q:=q^.left
D,A5AJj:]0else q:=q^.right;
`8P\V&e0treesrh:=q;
t:` jf6x0end;51Testing软件测试网5W$@2w$ZL&g"b3J
51Testing软件测试网I'J1O r#d dz.G:q

.jXuG,_&Z.kr_E017.KMP算法51Testing软件测试网$k#J zUZ9t4v*T W

VuygS5C~k YY018.贪心
icG5~k%hg3E;|G$I0*会议问题
M2@*K0_Y0(1) n个活动每个活动有一个开始时间和一个结束时间,任一时刻仅一项活动进行,求满足活动数最多的情况。51Testing软件测试网{X'R:G1g'b.PLj4T
解:按每项活动的结束时间进行排序,排在前面的优先满足。51Testing软件测试网/Z(O y@~

chS h C1@.^^4b1{n1b0(2)会议室空闲时间最少。
/iiA9glI;}.iiR051Testing软件测试网Z1_ ]d,E5h"~+~&aT
(3)每个客户有一个愿付的租金,求最大利润。51Testing软件测试网c8nz.JtD
51Testing软件测试网8r;W+`,U%|k8_)H0guc
(4)共R间会议室,第i个客户需使用i间会议室,费用相同,求最大利润。
e'kpTm"U|V,K^x$t051Testing软件测试网(_8r&R.Vi&l
附录1 常用技巧
P+v5Xa H9C K.W01.带权中位数51Testing软件测试网xO6_'w7^$^
我国蒙古大草原上有N(N是不大于100的自然数)个牧民定居点P1(X1,Y1)、P2(X2,Y2)、 …Pn(Xn,Yn),相应地有关权重为Wi,现在要求你在大草原上找一点P(Xp,Yp),使P点到任 一点Pi的距离Di与Wi之积之和为最小。   
R VG[$B7a2m,W#Z0   即求 D=W1*D1+W2*D2+…+Wi*Di+…+Wn*Dn 有最小值   
3ZW0^/lIG0结论:对x与y两个方向分别求解带权中位数,转化为一维。
t#|d'f9d\0设最佳点p为点k,则点k满足:51Testing软件测试网%Udw(qU?
令W为点k到其余各点的带权距离之和,则51Testing软件测试网~o-r0A VHo+tR
sigema( i=1 to k-1) Wi*Di < = W/2
*S,v2b2WW ]_0sigema( i=k+1 to n) Wi*Di < = W/2
,dy}n!n%z#A)H0同时满足上述两式的点k即为带权中位数。51Testing软件测试网[ y O$?$x2Y[
51Testing软件测试网 m.Q \ m [| oc
2.求一序列中连续子序列的最大和
]*CO(X*Ci0begin51Testing软件测试网*jt1v)P1N9V
maxsum:=-maxlongint;51Testing软件测试网9P+Hr,?4]"[_4l
sum:=0;
/J+jx c/h_.~.WSzD@0for i:=1 to n do begin
@+D2J!qR0inc(sum,data[i]);
#Ey1VKO X0if sum >maxsum then maxsum:=sum;51Testing软件测试网i"` t)x9UdX/]G3n
if sum< 0 then sum:=0;51Testing软件测试网 HG6a#Wv o9?Z$?
end;51Testing软件测试网'w*JP t$ti
writeln(maxsum);51Testing软件测试网4z|Q AZ,eU,rvN2V
end;
c)?f_!U)b$VD0

TAG: Delphi编程

 

评分:0

我来说两句

Open Toolbar