cau truc dl va giai thuat

Màu nền
Font chữ
Font size
Chiều cao dòng

I _SX Õi ch× liên ti¿p(sçi bÍt):

1. gi£i thu­t.

For i :=1 to n-1 do

For j:=1 to n-i do

ì (xi < xj ) then

{ tg:=xi;

xi:=xj;

xj:=tg;

}

2. thç tåc:

type

day=array[1..100] of real;

procedure sx(n: integer;var A:day)

var i ,j:integer;

tg: real;

{ for i :=1 to n-1 do

for j:=1 to n-i do

if x[j]>x[j+1] then

{ tg:=x[j];

x[j]=x[j+1];

x[j+1]= tg;

} }

3. ch°¡ng trình.

Program ctsx;

Type

Day=array[1..100] of real;

procedure sx(n: integer;var A:day)

{ write ( nhap vao so phan tu cua day );

readln (n);

write ( nhap mang );

for (i :=1 to n do

write ( a[ i ]);

readln (a[i]);

sx (n,a);

write ( day vua sap xep la );

for i :=1 to n do

write (a[1]:6:2);}

II_ chÍn trñc ti¿p(:

1. giai thuat:

for i :=1 to n-1 do

{ m:=i ;

for j:=i +1 to n do

if (Xm>Xi) then

m:=j; tg:=Xm;

Xm:=Xi; Xi:=tg; }

2. thç tåc:

type

Day=array(1..100) of rael;

procedure sx(n: integer;var A:day)

var m,i,j,n :integer;

tg: real;

begin

for i :=1 to n-1 do

{ m:=i

for j:=i +1 to n do

if (Xm>Xj) then

a:=j; tg:=Xm;

Xm:=Xi; Xi:=tg;}

3. chuong trinh

program ctud;

Type

Day=array[1..100] of real;

procedure sx(n: integer;var A:day)

var n,i ,j: integer;

{ write (' nhap so pt cua mang');

readln (n);

write (' nhap vao 1 mang');

for i:=1 to n do

write (a['i'] );

sx(n,a);

write ('mang sau sx la');

for i :=1 to n do

writeln (a[i]); }

III_ PP chèn:

1.gi£i thu­t:

for i :=2 to n do

{ t:=Xi; j:=i -1;

while (Xj>t) and (j=0 ) do

{ Xj+1:=Xj; j:=j-1;}

Xj+1:=t; }

2. thu tuc:

type

Day=array(1..100) of rael;

procedure sx(n: integer;var A:day)

var i,j,t :integer;

{ for i :=2 to n do

t:=A[i]; j:=i -1;

while (A[j] >t) and (j>0) do

{ A[j+1]:=X[j]; j:=j-1;}

X[j+1]:= t; } }

3. chuong trinh

program ctud;

Type

Day=array[1..100] of real;

procedure sx(n: integer;var A:day)

var n,i ,j: integer;

{ write (' nhap so pt cua mang');

readln (n);

write (' nhap vao 1 mang');

for i:=1 to n do

write (a['i'] );

sx(n,a);

write ('mang sau sx la');

for i :=1 to n do

writeln (a[i]); }

IV_ PP chia ôi :

1.gi£i thu­t:

quick_sort (S,L,R)

if (L<R) then

{ k:=(l+r) div 2;

t:=S[k]; i :=l;

j:=r;

repeat

while (s[i ]<t) do i:=i+1;

while (S[j]>t) do j:=j+1;

if (i <j) then

{ tg:=S[i ];

S[i }=S[j]; S[j]:=tg;

If (i <=j) then

{ j:=i +1; j:=j+1; }

until (i >j);

if (l<j) then quick_sort(S,L,J);

if (i<r) then quick_sort(S,i,R);}

V/ x¿p theo nguyên t¯c vun Ñng

Gi£i thu­t :

Hoán_vË_cha_con(x,l,r)

if (l<>lá) va (có gtrË nhÏ h¡n con) then

begin

chÍn con co gtrË lÛn h¡n xj

Õi ch× xl và xj

gÍi hoán_vË_cha_con(x,j,r);

end;

end;

pro2: t¡o_head_dau(x,n);

for i :=n div 2 dowto 1 do

hoan_vi_cha_con(x,i ,n)

end;

pro3:

begin

tao_head_dau(x,n);

fo i :=n downto 3 do

begin

Doi cho: xl, , xi

Hoan_vi_cha_con(x,l,i );

End;

End;

Thu tuc:

Type

Day=array[1..50] of integer

n,x,r: integer;

procedure hoan_vi_cha_con(x,l,r);

var i ,j,tg:integer;

{ while(l=r div2) do

{ j:=2*l;

if (j<r)and (Xj<Xj+1) then

j:=j+1;

if Xi<Xj then

{tg:=Xl;

Xl:=Xj; Xj:=tg; }

Hoan_vi_cha_con(x,j,r);

L:=j; } }

Procedure tao_heap_dau(x,n)

{ for i :=n div2 downto 1 do

hoan_vi_cha_con(x,i ,n); }

procedure tao_heap_sort(x,n)

{ tao_heap_dau(x,n);

for i :=n downto 3 do

{ tg:=X1;

X1:=Xi; Xi:=tg;

Hoan_vi_cha_con(x,l,i ); }}

BAI 1

Hãy t¡o mÙt dãy sÑ hai liên k¿t mà m×i ph§n tí là mÙt sv gÓm các thông tin:masv, hoten, namsinh,tb,xlht.

Yêu c§u : viÇc nh­p x½ dëng l¡i khi ng°Ýi có masv là r×ng.ph£i nh­p sao cho hai ng°Ýi ko trùng mã.

In danh sách ra màn hình sao cho m×i ng°Ýi trên mÙt dòng.

Hãy sx danh sách theo chiÁu tng d§n cça hoten

Nh­p vào TT cça mÙt sv mÛi. Thêm sv này vào ds trên sao cho ko làm thay Õi. Hãy soá bÏ nhïng ng°Ýi có dtb<=3

Ch°¡ng trình:

Type object1=record;

Masv: string[10];

Ht: string[25];

Ns: integer;

Dtb: real;

Xl: string[10];

End;

Tro =^ node

Node=record;

Infor:object;

Prev,next: tro;

End;

Var: lc,ld:tro;

Function test(ld:tro,ma:string):Boolean;

Var P:tro found:Boolean;

{ p:=ld;

found: =false;

while (p<>null) and (not found) do

if p^.infor.masv=ma then

found :=true;

else

P:=p^.next;

Test:=found;

End;

Procedure nhap(lc,ld:tro);

Var x:object;

{ repeat

write('nhap vao ma sinh vien');

readln(x.ma);

if test(ld,x.ma=found) then

writeln('moi ban nhap lai ma:');

else

write(' moi ban nhap lai ho ten');

readln(x.ht);

writeln('moi ban nhap lai ngay sinh);

readln(x.ns);

writeln('moi ban nhap lai diem trung binh');

readln(x.dtb);

if (x.dtb>=8)then

write('gioi')

else

if(x.dtb>=6,5) and (x.dtb>=8)then

write('kha');

else

if(x.dtb>=5,0) and (x.dtb>=6,5)then

write('trung binh');

else

write('kem');

bosung2 (ld,lc,x);

until (x.ma=' ');

end;

procedure in(ld:tro);

var p:tro;

i :=1

begin

p:=L1;

while p<>null do

begin

with p^.infor do

write('masv':10,'ht':25,'ns':4,'dtb':4,'xl':10);

p:p^.next;

i :=i +1

end; end;

procedure chen(ld,lc:tro)

var q:tro;

x:object;

begin

q:=ld;

nhap(ld,lc);

while (q<>null) and (q^.infor.ht<x.ht)do

q:=q^.next;

if q=null then

bosung2(ld,lc,x)

else

bosung4(ld,lc,x,q);

procedure xoa(var ld,lc:tro);

var p:tro;

{P:=ld;

while p<>null do

if (x.dtb<=3) then

loaibo(ld,lc,p)

else

p:=p^.next;

end;

bài t­p 2

t¡o mÙt ds các tài s£n là mÙt ngn x¿p mà m×i ph§n tí là mÙt thông tin: mats,ten,sl,dvt,hsd,tìnhtr¡ng.

in ds tài s£n h¿t h¡n sí dång

thêm vào tài s£n mÛi tho£ mãn iÁu kiÇn tài s£n có sÑ l°ãng ít nh¥t.

Xoá nhïng ds có tình tr¡ng hÏng n·ng

sx theo chiÁu tng d§n cça sÑ l°ãng

Gi£i :

Uses dos,crt,strack;

Getdata(var d:data);

Gerdata(d): d.day,d.month,d.year;

Type

Object=record;

Mats: string[10];

Tents: string[25];

Dvt: string[5];

TT: string[5];

Sl, hsd:integer;

End;

Tro=^node;

Node=record;

Next:tro;

Infor:object;

End;

Var s: tro;

Procedure input(s.tro;x.object)

{ creat (s);

repeat

writeln('nhap vao ma tai san');

readln(x.mats);

if x.mats<>' ' then

{write('nhap ten tai san');

readln(x.tents);

tuong tu nhap sl, dvt,hsd,tinh trang;

bosung(s,x);

end;end;

procedure printe(var s:tro);

var x:object;

{while s<>null do

laypt(s,x);

if x.hsd>2005 then

{write(x.mats,3);

writeln(x.sl);

writeln(x.dvt);

writeln(x.hsd);

writeln(x.tt); } }

procedure them(var s:tro);

var m,min,x:object

p,q:tro;

{pop(s,x); min:=x.sl;

DÈÊ<

f

#1g

ŒÌ¨ªÀÄÌÎNR'º! !>!@!'

(

(:*.JþKL†NºSÐSøV] ]ddïàÐàïàïàïàïàÐàÐàÐàÐàïÀ®À®àïàÀà¬àïÀàïÀàïàŸh[9¥hT%¼OJQJaJU"h[9¥h<[6�>*CJOJQJaJ-h[9¥h<[>*CJOJQJaJ-h[9¥h<[CJH*OJQJaJh[9¥h<[CJOJQJaJ-h[9¥h<[5�CJOJQJaJ)DdŒ'Øð 0 < r ¸ ä ú &

N

v

¬

Æ

Ü

þ

$

Z

ô

÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[dþô

8

^

'

ò

<

f

¬

¾

æ

" F ^ j ¢ è (4

-¶÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[¶Òô

8[p'žºËÜåÿ#1 8\'¼Ôìø0÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ëëëëëë÷÷

$

Æ„a$gd<[ $a$gd<[0v Èì , > N \ a | Ÿ ' Ö â þ )CUg

*Pl�°÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[°¼Ì6TnœÀêHŒÌèht®Ò

P†¼ÆÒÞ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[Þ.:^�š¤¶Àø

/EZi...�›¥·Ïàü4Qf|÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[|†-'ºÊ'- d @!\!ˆ!¬!Ì!æ!ü! ""-",":"J"O"_"ˆ"¡"ª"¹"Ü"÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[Ü"õ"## #!#A#O#X#x#†#¢#Ã#È#ë#ù#-$-$Z$i${$‰$Ž$°$¾$Ã$ç$ü$÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[ü$%%"%5%:%P%[%a%g%n%%...%-%Ç%Ò%Û%å%ÿ%

& &&!&.&W&c&r&ƒ&ˆ&÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[ˆ&œ&º&Å&Í&Þ&ñ&''''

(Ú()¦)ò):*H*r*œ*à*ì*

+.+T+t+'+'+¾+÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[¾+Ô+î+,, , ,@,M,T,t,„,š,¶,Ç,í,ú,- -.-@-L-_-q-€-�- -³-Î-÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[Î-á-ê-.J:JPJxJŽJ¤JÂJèJòJKK,KNKlK‚K KÊKâKøKþKLbLˆM¼MN÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ $a$gd<[push(q,x);

while(x<>null) do

pop (s,x);

if (x.sl<min) then

min:=x.sl;

push(q,x);

if s=null then

writeln( ds rong )

else

m:=min(s);

repeat

pop(s,x);

if (x.sl=m) then

push(s,dsmoi);

push(q,x);

until (x.sl=m)

while not emply(q)do

{pop (q,x);

push(s,x);

}}

BAI TAP3

Vi¿t mÙt ch°¡ng trình làm các viÇc sau:

Nh­p vào mÙt danh sách cça nhïng ng°Ýi i mua g¡o là mÙt hàm ãi trong ó m×i ng°Ýi có mÙt phi¿u. Mua g¡o gÓm các TT: sophi¿u, tên ng°Ýi mua,SNK.

In ra màn hình danh sách;

Lo¡i bÏ ng°Ýi có phi¿u (=0) ngày mua tr°Ûc 20/11

Tìm và in ra màn hình nhïng ng°Ýi có nhân kh©u >=5.

Gi£i

Count max=100;

Type

Data = record;

Ng,th,nam:integer;

Odject= record;

Quece= record;

Front,rear: 0. maxq;

E: array[1..maxq] of obiect

phieu= record;

Sp,sonk,sgm: integer;

HT: string[25];

Ngmua:date;

Quece= record

Font, rear:0..maxq;

E:array[1..maxq] of phieu

End;

Procedure nhap(var q:quece);

Var x: phieu;

{ repeat

write( nhap vao so phieu nguoi mua );

readln(x.sp);

write( nhap vao ho ten nguoi mua );

readln(x.ht);

tuong tu nhap nk.sogao,ngay mua;

until (x.sp=0);}

procedure del(var q:quece, var x:phieu);

var q1:quece;

{ while not emptyq(q) do { delq(q,x);

if(q.nm.ngnam>20/11) then

addq(q1,x)

else if(q.hg.mua.nam=20/11) then

addq(q1,x)}

q:=q1;}

BAI TAP 4

Vi¿t mÙt ch°¡ng trình thñc hiÇn các viÇc sau

Nh­p të bàn fím mÙt ds tuy¿n tính có n ph§n tí m×i ph§n tí là mÙt phân sÑ có tí và m«u sÑ là sÑ nguyên d°¡ng

In dãy phân sÑ ra màn hình ß d¡ng tí sÑ trên m«u sÑ

Nh­p mÙt sÑ nguyên k ? trong dãy phân sÑ trên có phân sÑ nào mà ph§n nguyên cça k¿t qu£ chia b±ng k

Sx dãy phân sÑ theo chiÁu tng cça tí sÑ ,n¿u b±ng theo chiÁu gi£m d§n cça phân sÑ

In ds da sx

Gi£i :

Const max=100;

Type

Object=record;

T,m:integer;

end;

list=record;

e=array[1..max] of object

count: 0..max

end;

var l:list

procedure timkiem(tu:integer;mau:integer);

var i ,k: integer;

{for i :=1 to l.count

if k=l.e[i].tu div l.e[i].mau then

writeln( da tim thay phan tu o vi tri thu ,i ); }

procedure nhap(tu:integer;mau:integer;fso:integer);

{ writeln( nhap vao tu so );

readln(tu);

writeln( nhap vao mau so );

readln(mau);

write( phan so vua nhap la ,tu / ,mau); }

procedure sapxep(tu:integer;mau:integer);

var tg;m,i ,j:integer;

{ for i :=0 to l.cout -1 do

m:=i ;

{ for j:=i+1 to l.cout do

if(l.e[m].tu>l.e[j].mau) then

m:=j else

if (l.e[m].tu=l.e[j].tu) then

if (l.e[m].mau=l.e[j].mau) then

{ m:=j

tg:=m.e[m].tu;

m.e[j].tu:=m.e[m].tu;

m.e[j].tu:=tg; }

vd 1

các phép toan trên ngn x¿p:

1-Khßi t¡o ngn x¿p r×ng:

procedure creater(var s:stack);

{ s.top=0;}

2-KiÃm tra tính r×ng cça ngn x¿p:

function emptys(s:stack): Boolean;

{ emptys:=(s.top=0)}

3-KiÃm tra tính §y:

function fulls (s:stack): Boolean;

{ fulls:=(s.top= max.stack)}

4-BÕ sung mÙt ph§n tí vào §u ngn x¿p:

procedure push(var s:stack;x:object);

{ if fulls(s) then writeln( s day du )

else { s.top:=s.top=1;

s.e[s.top]:=x; } }

5-L¥y 1 PT ß §u ngan x¿p:

procedure pop(var s:stack;x:object);

{ if emptys(s) then writeln( s tro nen rong )

else { x:=s.e[s.op];

s.top:=s.top+1; } }

VI- tìm ki¿m:

1.TKTu§n tñ:

type

day=array[1..100] of integer;

functionTKTT(x:day;k:real;n:integer)BOOLEAN;

var i : integer;

tl:Boolean;

{ tl:=false;

i :=1;

k:=x[n+1];

while (1>=n)and(not tl) do

if(x[i]=k then

if(i <n+1)then

tl:=truce

else

i :=i +1;

tktt:=tl; }

Bạn đang đọc truyện trên: Truyen2U.Pro

#ctdlgt