1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

bài tập bài toán chiếc túi xách

12 506 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 12
Dung lượng 3,51 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Bd6c 5: Cii det chddng trinh Program XauChung; uses crt; Var L:Array[0..

Trang 1

a Ph5t bidu bdi to5n:

Bii tQp 1: Bii toSn chi€'c triix5ch G\/

MOt c6i kho chfa n loai d6 v6t c6 kich thr/6c vd gi5 tri khiSc nhau Cu thd: Loai db vgt i

(i=1 n) c6:

- Kich cd m[i] e N.

- Gi5 tri c[i] e R

- Sd lrtOng: khdng han ch6

M6t t6n tr6m mang theo chi6c t[i c6 kich cd ld p e Nx V6y h6n ph6i chon lda mdt danh siich

ciic db vdt s6 mang di nhd th6 nio dd cho tdng gi5 tri t6y c5p dddc ti t6n nhdt

Tfc: Tim x[1], x[2],,,,, x[n] (vdi x[i] e N : sd lLtdng loai db v6t thf icen ldy) sao cho: ,;

Ix[i].mlil < p ve lx[i].c[i] dat gi5 tri ctrc dai

x Input: + n ld sd d6 v6t, kich thd6c cdc db khSc nhau.

Vdi m[i] e N' , c[i] e R

+ p lA cd trii

x Output: Tim (X[1], X[2] X[n]) (X[i ] :sdtoaidbv6t i, i=1 n)

5ao cno:

tI'*t,i.t,t-+ max

n

r' Vidt1: n = 5 ; p = 10, dtJdccho

x K€t guir: + Sd db virt 5, kich thd6c 1"0

+ Tdng gi5 tri nh6p dr/dc vdo t(i xSch: B

+ Sd lrrOng ldy theo th[r tlr nhd sau: 0 0 1 1 0

b Ph6n tich bii to6n:

Brl6c 1: Ph6n tich bii toSn:

Goi P(r, s) li bdi todn chi6c tfi x6ch, vdi:

r e Nx: kich cd chi6c t[i

s e N*: s6 c5c loai d6 v6t khdc nhau (bdi todn ban d6u td p(p, n))

C5c gi5 tri cEn tim:

t[r,s]: gi5 tricdc dai Ixlil,c[i] cia bditoSn p(r, s)

[r,s]:_s6 h/dng loaidb v6t s t6i uu c6n l6'y (tr?c: x[s]) ctia bditodn p(r, s)

Brfdc 2: ciii phSp ct€ quy:

Khis=1:

u[r, 1] = r div m[1]

Ilr, 1l = u[r, 1]*c[1]

Khis>1:

l[r, s] = max { k*c[s] + lfr - kxmfs], s-11 ]

0<k<rdivm[s]

= koxclsl + lfr - h*mls], s-11

ufr, s] = ks

Brldc 3: LQp bing

Procedure LapBang;

Begin

For s:=1 to n do

For r:=0 to p do

If s=1 then Tinh u[r, 1] vir l[r, 1]

else Tfnh u[r, s] vd l[r, s];

End;

Vi du

Trang 2

R

x x€'t qui: + Sd d6 vat s, kicfr thrrdc to

+ Tdng gi6 tr! nh6p dUdc vdo tfixiich: B

+ Sd luOng ldy theo thf/ tLr nhut sau: 0 0 1 1 0

D0 phrirc tap tinh to6n: O(np2)

BrJ6c 4: Tdng hqrp k€t qui

Procedure TongHop;

Begin

r: =pi

For s:=n downto 1 do

Begin

x[s]:= u[r,s];

r:= r - x[s],mfsl;

End;

End;

E6 phr?c t?e tinh to6n: O(n)

BrJ6c 5: Cei dit chddng trinh

Program TuiXach;

Uses crt;

Var n,p:word;

x,m:arrayf1., 10] of word;

c:arrayf1 10] of real;

l:array[0 10,1 10] of real;

u:array[0 10,1 10] of word;

Procedure NhapDL;

Var i:byte;f:text;

Begin

assign(f,'tuixach.inp'); reset(f);

read(f,n); readln(f,p);

End;

Procedure Lapbang;

Var r,s,k,k1 :word; max,tam : real;

Begin

for s:=1 to n do

for r:=0 to p do

if s=1 then Begin

u[r,1]:= r div m[1];

l[r,1]:=uIr,1]*cl1];

End else Begin k1: =0;

max:= 0;

for k:=0 to r div m[s] do Begin

for i:=1 to n do read(f,mlil);

for i:=1 to n do read(f,c[i]);

writeln('So vat ',n,', kich thuoc

lieu trong file tuixach.inp');

close(f);

tui 'rp,', du

Trang 3

Tam : = kxclsl +lIr-kxm[s],s-11;

if tam>max then

Begin

Max:=tam; k1:=k;

end;

end;

lIr,s]:=max;

u[r,s]:=k1;

End;

End;

Procedure tonghop;

Var r,s:word;f:text;

Begin

assig n(f,'tuixach.out');

rewrite(f);

r:=pj

for s:= n downto 1 do

Begin

xlsl: =uIr,s];

r:=r-x[s]xm[s];

End;

End;

Procedure Inkq;

Var s:word; f:text;

Begin

assig n(f,'tu ixach.out' );

rewrite(f);

writein('Tong gia tri nhan duoc rrao tui xach

la: ',1[p,n]:4:1);

write('So luong lay theo thu tu nhu sau: ');

for s:=1 to n do

Begin

write(f,xIs]:3);

write(x[s]:3);

End;

writeln;

close(f);

End;

Procedure Inbang;

Var srr:word;

Begin

writeln;

for s:=1 to n do write(s:S,' ');

writeln;

for r:=0 to p do Begin

write(r);

for s:=1 to n do Begin

write(lIr,s] : 3 : 0,'\', u Ir,s],");

End;

writeln;

end;

End;

t - -)

BEGIN Clrscr;

NhapDL;

Lapbang;

Tonghop;

inbang;

Inkq;

Readln;

END.

{ - -}

BiitQp 2: ph6p nhAn td hdp nhi€u ma trQn (O", )

\J

a PhSt bidu bii to6n

CEn tinh M = M1xM2x ,xMn

Trong d6: M, Id ma tr6n c5'p m[i-1]xm[i] (i=1 n)

H5y xdc dinh thf tUthr/c hi6n cdc ph6p nh6n sao cho s6 ph6p tinh Ie t6ithidu

x Input: + C6'p cr]al ma tr6n

Vdi m[i] e N

x Output: X5c dinh thf td tinh: sd ph6p tinh > min

xVidg:TinhM= M1 * MZ " M3

| 10x201[20xs0] [s0xs]

(MrxM2)xM, c6 sd ph6p todn td:

10x20x50 + 10x50x5 = 12500 M1x(M2xM3 ) c6 sd ph6p todn ld:

10x20x5+20x50x5=6000

x K€'t qui: M1 x M2 x M, c6 sd ph6p todn lir: 6000

b PhAn tich bii to6n:

Bddc 1: Ph6n tich bii toin:

Goi P(r, s) ld bdi todn nhdn ma tr6n: MrxMr*rx xM' vdi r < s

cua n ma tr

Trang 4

(bai todn ban d6u td p(1, n))

Gi5 tric6n tim:

!lt,r]t vitri ph6p to5n thrrc hi6n cudi cing cia beitodn p(r, s)

(MrxMr*1x xMp-1) x (MpxMp*

1" rM5)

k = k[r,s] e [r+1, s]

[r, s]: sd ph6p tinh nh6n t6i uu cira baitodn p(r, s)

BtI6c 2; ciii ph5p dG quy:

Trr/o'ng hdp 1: Khis = r: l[r, r] = g

Trr/dng hgp.2t KhL5 = p1g.

lt r,Q t\< hf'- r fx m Jf.1 J I q 1

kfqv+11 =-1+1

Trddng hdp 3r Khi s > r<S:

l[r, s] = r+1 min <v<s { l[r, v-1] + m[r-1]*m[v-1]*mlsl + lfv, sl ]

(v lA c5c vi tri ph6p todn thdc hi6n cudi cirng khdc nhau)

= l[r, vL1] + mlr-11 * mlvll] {, m[s] + l[v', s]

(v' ld vi trit6i rru trong sd cdc vi tri cira v) k[r, s] = Y'

Nh6n X_6t: C6 thd gh6p trddng hdp 2 vAo trddng hdp 3l

Brfdc 3: Lflp bing

Procedure LapBang;

Begin

For s:=1 to n do

For r:=s downto 1 do

If r = s then lfr, r] := 0 End; else Tinh l[r' s] vi k[r' s];

Vi^!U: Tr/ vi du tr6n ta l6p dddc bing sau

56 ph6p to6n tdi thidu td: 6000

Thf t{thrJc hi6n ph6p nh6n: 3 2

r

D6 phr?c t?p tinh to6n: O(n3)

Brf6c 4: Tdng hep k6t quJ

Procedure TongHop;

Begin

writetn('Sd ph6p tinh le t6i thidu:,, l[1, n]);

i:=r'ti

1-imW(1, n); {NhEm lEn [fdt xiic dinh cdc gi5 tri x[i]: vi tri ph6p nhSn cEn thuc hi6n trong tEn

nh6n thf i(i=1 n-1))

writeln('Thr? t{ thttc hi6n cdc ph6p nh6n :,);

For i:=1 to n-1 do writeln(xfil);

End;

Trong d6 th[r tuc TimW dr/dc x6y dqfng d9 quy nhf sau:

Procedure TimW(r, s);

Begin

If (r < s) then

Begin

i:=i-1;

vt:= k[r, s];

xlil:= vt;

TimW(r, vt-1);

Trang 5

TimW(vt, s);

End;

End;

Bddc5: CAi d{t chtldng trinh

Program Matran;

Uses Crt;

Var n:word;

m :array[0 20] of word;

x:arrayf1 20] of word;

k,l :arrayf1 20,1 20] of integer;

{ -}

Procedure NhapDl;

Var i:byte;f:text;

Begin

assign(f,'matran, inp'); reset(f);

readln(f,n);

for i:=0 to n do

Begin

read(f,mIi]);

write(mIi]:3);

End;

writeln;

close(f);

End;

{ -}

Procedure Lapbang;

Var r,srvrvmax:word;

Begin

for s:=1 to n do

for r:=s downto l" do

if s=r then

Begin

lIr,r]:=0;

klr,rl:=r;

End

else

Begin

VmaX:=f + 1;

l[r,s]:=l[r,r]+mIr-llxmIr]xm[s] +lIr+ 1,s];

for v:=r+2 to s do

if

l[r,s]>l[r,v-1]+m[r-1]xm[v-llxm[s]+l[v,s] then

Begin

VMaX:=V;

llr,sl : = lIr,v- 1] +mIr-1]

xmlv-llxm[s]+l[v,s];

End;

k[r,s]:=vmax;

End;

End;

{ -}

Proced ure timvt( r,s :word) ;

Var vt,i:word;

Begin

if r<s then

Begin

i:=i-1;

vt:=k[r,s];

xlil:=vt;

timvt(r,vt-1);

timvt(vt,s);

End;

End;

t -)

Procedure tonghop;

Var i:word;f:text;

Begin assign(f,'matra n.out'); rewrite(f);

writeln('so phep toan toi thieu la: ',1[1,n]);

writeln(f,'so phep toan toi thieu la: ',1[1,n]);

i:=rtj timvt(1,n);

write('thu tu thuc hien cac phep nhan:'); writeln(f,'thu tu thuc hien cac phep nhan:'); for i:=1 to n-1 do

Begin

write(xIi]:3);

write(f,xIi],' ');

End;

Close(f);

End;

{ - -}

Procedure Inbang;

Begin

for s:=1 to n do write(s:10,' '); -j writeln;

for s:=1 to n do Begin

write(sr' ');

for r:=1 to s do t

Begin

write(lIr,s]:5 ,'f' ,klt,s],' ');

End;

writeln;

end;

End;

BEGIN

Clrscr;

NhapDL;

Lapbang;

Tonghop;

Inbang;

Readln;

END.

Trang 6

Bii tdp 3: Tim x6u trong cdc dai.

a PhSt bidu bii to6nr

Bei toSn x6u t1o1s cr"lc dqi: s ld x6u trong cia T n6u s nh6n dddc bSng cSch xo5 di m6t sd t<f

ttl nAo d6 Vidu: 'ABC, ld x6u trong cria.GAHEBOOC,

Bei todn: cho 2 x6u T1, T2 Tim m6t x6u S ld x6u trong chung cria T1 vd T2 co d6 ddi ct;c dAi,

'-"r -"errJ vev

'r ve

Vi du: TI='ABCDAE'vd T2='XYACADK' c6 x6u 'AcD' le x6u trong chung vdi d6

ddi cdc dai

* Input: X6u T1 vd T2

* output: X6u s li x6u trong chung cira T1 vir T2 c6 d6 dei cdc dai

b PhAn tich bii tof n:

Budc 1: phin tich bii toin:

Goi n lA d6 dei cria x6u T1

m ld d6 ddi c[ra x6u T2

Ta cEn xdc dinh x6u con chung S ddi nh6't c0a 2 x6u T1 vd T2

Gi6 sir L(i,j) c6 d6 dei x5u con chung ldn nh6't crla z x6u T1 c6 d6 dii i vd x6u T2 c6 d6 ddij

(i<=n, j<=m).

Btt6c2= ciii ph6p d6 quy:

+ Ndu i=0 thi L(0,1;:9, Ndu j=g thi L(i,0)=0

+ Ndu i>0 vd j>O va T1;=72-thi L(ij)'= i + L(i_1j_1)

+ N6u i>0 vd j>0 vd T1i<>T2j tni L(i;l = rvaxiLli,j_rj,l1i_r,i;;

Brf6c 3: L6p bing;

Procedure LapBang;

Begin

For i:=1 to n do

L[1,0]; =0;

For j:=1 to m do

L[O,j]: =0;

For i:=1 to n do

For j:=1 to m do

if T1[i] = T2[] then

L[i,j]:= 1 + Lli_1,j_11 else

L[i,j] := maxab(L[i,j_1], L[i_1,j]);

End;

Vi du: cho hai x6u T1='ABCDAE'vd T2-'XyACADK, Tim

x6u con chung c6 d6 dii rdn nh6.t.

BA

I

L * c.D

4

,

G -, \T;

A

TZ

l 1

?=

,&

Trang 7

Kdt qu6 x6u chung ddi nh6't ld: ACD

D0 phr?c tap tinh to5n: O(n.m)

Brl6c 4: Tdng h{p k6t qui:

Procedure Tonghop;

Begin

i:=hi j:=m; S:=";

While (L[i,j]>0) do

Begin

if T1[i]=T2Dlthen

Begin

S:=T2Ul + S;

:._; {,

l,=1;'

End else

if L[i, j]= Lli-1, jl then

i:=i-1

else

j: =j- 1;

End;

Writeln('Xau chung dai nhat:', S);

End;

D6 phfc tap tinh to6n: O(n).

Bd6c 5: Cii det chddng trinh

Program XauChung;

uses crt;

Var

L:Array[0 100,0 100] of Byte;

s1,s2,kq: String;

m,n: Byte;

{ -}

Function maxab(a,b: byte) : byte;

Begin

if a>b then

maxab: =a

else

maxab: =b;

End;

{ -}

Procedure NhapDL;

Var f : Text;

Begin

Assign(f,'xauchung, inp'); Reset(f);

Readln(f, s1);

Readln(f, s2);

m:=Length(s1);

n: =Length(s2);

Close(f);

End;

{ -}

Procedure LapBang;

Var i,j: Byte;

Begin

For i:=1 to n do

lIi,0]:=0;

For j:=1 to m do

ll0,jl:=0;

For i:=1 to m do For j:=1 to n do

if sllil = s2ljl then

L[i,j]:= 1 + L[i-1,j-1]

else

L[i,j]:= maxab(L[i,j-1]

1,il);

End;

{ -}

Procedure Tonghop;

Var

i,j:byte; f:text;

Begin Assi gn(f,'xauchung out' ); Rewrite(f);

i: =mi

j: =n;

kq:="'

While (L[i,j]>0) do Begin

if s1[i]=s2Ulthen

Begin

kq:=s2[] + kq;

i:=i-1;

j:=j-L;

End else

if L[i, j]= L[i, j-1] then

,

Trang 8

else

i:=i-1;

End;

Writeln(f,'s1 - ',s1),

Writeln(f,'s2 = ',s2);

Writeln(f,'Xau chung dai nhat;', kq);

Close(f);

End;

{ -}

Procedure Inkq;

Begin

Writeln('s1 = ',sl)i

Writeln('s2 - ',s2),

Writeln('Xau chung dai nhat:', kq);

End;

t -) i

Procedure Inbang;

var i,j:byte;

writeln(' - LAp BANG -\;

write(");

for j:=1 to m do write(s2[]:4);

writeln;

for i:=1 to n do begin

wrlte(s1[i]);

for j:=1 to m do write(lli,jl:4);

writeln;

end;

End;

{ -}

BEGIN

Clrscr;

NhapDL;

Lapbang;

Tonghop;

Inbang;

Inkq;

Readln;

END.

\

a phSt bidu bii to6n: Bii tQp 5; Bii to5n ngr/di du lich

M6t ngfdi di tt/ thdnh phd 0 ddn thdnh ph6 n vd c6 thd di qua n-l thanh phd kh5c 1, 2,.,.,

n-1, theo 16trinh: 0-+ i1 - i2 _+ i;,-+ n, trong d6: O < t i t ik n,

Gi5 v6 c0a xe ditr/thdnh phd id6n thdnh ph6 j ta cgi,i1.

Tim m6t 16 trinh ttJ thdnh phd 0 d6n thdnh pr'ro n-rul"."r.)o tdng chi phf v€ gi5 v6 dat cdc tidu,

* Input: +:- ld thtinh ph6xudt phdt lrrr vrrr vs vro

+ s li dfch

+ Ma tr6n cli,jl (i,j =0 n) tuu trii chi phf

x output: L6 trinh ngSn nh6't trr thdnh prr6 o o8n tnirnh phd n.

b Ph6n tich bii todn:

Brl6c 1: Ph6n tich bii to6n:

x Goi P(r,s) lA bei todn du lich, vdi:

- r e N: dinh xudt phdt

- s e N: dinh cEn d6n

(bei todn ban dEu: p(0,n))

x C5c gi5 tri c6n tim:

- l[l,s] : chi phi nhd nh6't d6 di ti/ r _> s c0a bei todn p(r,s)

- ufr,sl : dinh k6 cu6i tr6n dL/dng di tf r-> s cira odi toJn p(r,s)

BtI6c2= Giii ph6p d€ qui:

-Khir=s:

l[r's] = 6

u[r,s] = -1 -Khi r + s:

l[r,s] = min(l[r,k] + c[k,s]) = l[r,ko]+c[ko,sJ

(r-<k<s) ulr,s] = ko

Brfdc 3: LAp bing:

Procedure Lapbang;

Begin

Fors:=0tondo

Forr: =0tosdo

Ifr=sThen

Begin

l(r, r): = 0;

u(r, r) = -1'

Trang 9

Else Tinh l(r,s)) vir u(r,s);

End;

Dfdng di tU thenh phd 0 ddn thdnh ph6 4 c6 t6 trinh: 0 >1 >2 >4

Chi phi nh6 nhdt dd di tiJ thdnh phd 0 ddn thdnh ph6 4: 15

D6 phr?c t?p tinh to6n: O(n3)

Brl6c 4: Tdng hdp:

Procedure Tong hop(d,c :word );

Begin

i:=0; x[0]:=c; k:=ci

while k<>d do

Begin

i:=i+1i xlil:=u[d,k];

k:=u[d,k];

End;

End;

D6 phr?c tap tinh to5n: O(n)

Brl6c 5: Cii d{c chr.tdng trinh

Program DuLich;

Uses cft;

Const max=100;

Var n:word;

x:array[0 max] of integer;

c,l,u:array[O max,O max] of integer;

{ - -}

Procedure NhapDL;

Var i,j:byte;f:text;

Begin

assign(f,'C: \d u lich inp'); reset(f);

readln(f,n);

for i:=0 to n do

for j:=0 to n do

read(f,cIi,j]);

close(f);

End;

Procedure Lapbang;

Var r,s,k,k0:word;

Begin

for s:=0 to n do

for r:=0 to s do

if s=r then

Begin

lIr,s]:=0;

u Ir,s]: =- 1;

End else Begin k0: =s-1;

lIr,s] : = lIr,s-1] +c[s- 1,s];

for k:=r to s-1 do

if l[r,s] > l[r,k]+c[k,s] then Begin

k0: = k;

lIr,s] : = lIr,k] +c[k,s];

End;

u[r,s]:=k0;

End;

End;

{ - -}

Procedure tonghop(d,c:word);

Var i,k,j,t:word;f:text;

Begin assig n(f,'C: \dulich.out');

rewrite(f);

i:=0;

s

-1

0

o -1

1

5

1

0 -1

0

7

1

4

2

0 -1

2

8

2

3

2

4

3

o -1

Trang 10

x[0]:=c; k:=c;

while k<>d do

Begin

i:=i+1;

x[i]:=u[d,k];

k: =u[d,k];

End;

writeln(f,'Duong di tu Tp ', d,, den Tp ',c,, chi

nho nhat la:');

for j:=i downto 0 do

Begin

write(f,x[]);

if j>0 then write(f,' >,);

End;

writeln(f);

write(f,'chi phi la: ',1[d,c]);

close(f);

End;

{ - -}

Procedure Inkq;

Var f:text; ch:string;

Begin

Assign(f,'C: \DuLich.Out'); reset(f);

readln(f,ch);write(ch);

readln(f,ch);writeln(ch);

read I n(f,ch);write(ch);

close(f);

End;

{ - -}

Procedure InBang;

Var i,j:Byte;

Begin

phi for i:=0 to n do write(i:g);writeln;

for j:=0 to n do

Begin

write(;':4,' ');

for i;=0 to j do

write(uIi,j],'/,,1[i,j],' ');

writeln;

End;

End;

BEGIN

Clrscr;

NhapDL;

Lapbang;

Tonghop(0,4);

Inbang;

Inkq;

Readln;END.

a phSt bidu bai toSn: Bia tip 5: Bii toSn sinh vi6n 6n thi,

Bei todn sinh vi6n 6n thi: M6t sinh.vi6n cdn

T ngdy dd 6n thi n m6n Theo kinh nghi6m cia anh ta, ndu 6n m6n j trong i ngdy thi drdc oidr'H J1i,;1 cia stJ

cho nia a a[i,j] (vdi

a[i,j] < =a[i+ 1,j])

Tim b6 xUl (sd ngdy 6n m6n j, v6i j=1,.n) sao cho Ix[1=, vd sinh vi€n dat tdng didm idn

nhdt (IalxUl, jl-+max)

x Input: + n ld sd m6n

+ m td s6 ngdy

+ aIij] didm crla 6n thi m6n j trong i ngiry (ali,jl<=a[i+1,j])

x out put: -,, r"J,]rJiT;';f; i'r,=I n) *o *orx[]=, va sinh vi6n dat tdns didm rdn

nhdt (IalxUl, jl-+max),

b Ph6n tich biito5n:

Brldc 1: ph6n tich bii to6n:

x Goi p(r,s) ld bii toiin SV 6n thi r m6n trong s ngEy

(bai to6n ban d6u s6 ld : p(n,m)) "

* Cdc gi5 tri cEn tim lir:

bait"drl[i;rrj.tunn sd didm ldn nh6't mir sinh vi6n c6 thd dat dudc khi 6n thi r m6n trons s ngdy cria _ ufr,gJ : 56 ngdy tdi uu cEn d6 6n m6n r c0a bii todn p(r,s)

Bttdc2: ciii phip dQ qui:

-flrif = 1:l[1,r] =a[s,1], u[1,s] = I

-Khi

i> 1:l[r,s] = max(attit it;r-r,s_t1;

a[h,r] + llr-1, s-ksl

Brf6c 3: L6p bing: u[r's] = ko

Procedure Lapbang;

Begin

for s:=1 to m do

for r:=0 to n do

End;

if (s =.1) then Tinh lf1,sl vd ufl,sl

else Tinh l[r,s] vi u[r,s]

vf du: c6 3 m6n thi 6n trong 5 ngdy vir didm cdc m6n tr/dng r?ng cdc ngdy nhf sau:

Trang 11

K6t qu6 l6p b6ng

Tdng didm ldn nh6't ld: 24.0

E0 phr?c t?p tinh to5n:

Brldc 4: Tdng hfp k6t qui:

Procedure Tonghop;

Begin

f: = ITli

for s:= n downto 1 do

begtn

x[s]:= ulr,s];

r:= r - x[s]i

End;

End;

D6 pht?c tap tinh to6n: O(n)

BrJdc 5: Cii dic chr^fdng trinh:

Program sinhvienonthi;

Uses Cft;

Const max=50;

Var n,m:word;td:real;

x:array[1 max] of word;

a :arrayfO.,max,0 max] of real;

l:array[0 max,0 max] of real;

u :array[0 max,0 max] of word;

{ - -}

Procedure NhapDL;

Var i,j:byte;f:text;

Begin

assign(f,'sinhvien, inp'); reset(f);

read(f,n); readln(f,m);

writeln('So mon ',n,'on trong ',m,' ngay');

writeln('Diem tuong ung on cac mon la:');

for i:=0 to m do

begin

for j:=1 to n do

begin

read(f,aIi,j]);

write(aIi,j]:4:0);

end;

readln(f);

writeln;

end;

close(f);

End;

{ - -}

Procedure Lapbang;

Var r,s,k,k0:word;

Begin

for s:=0 to m do

for r:=1 to n do

if r=1 then Begin

u[1,s]:= s;

l[ 1,s]: =a [s,1];

End else Begin k0: =0; l[r,s] : =l[r-1,s]; for k:=0 to s do

if lIr,s] <lIr-1,s-k]+a[k,r] then Begin

k0: =k;

I Ir,s] : = l[r-l,s-k] +a Ik,r] ; End;

u[r,s]:=k0;

End;

r

o(

Ngày đăng: 02/06/2015, 10:39

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w