cau truc dl va giai thuat
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 tăng 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 ngăn 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 tăng 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;
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 tăng 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 ngăn xếp:
1-Khởi tạo ngăn xếp rỗng:
procedure creater(var s:stack);
{ s.top=0;}
2-Kiểm tra tính rỗng của ngăn 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 ngăn 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: AzTruyen.Top