Tải bản đầy đủ (.pdf) (15 trang)

Bai 1 cau truc du lieu va giai thuat

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (95.83 KB, 15 trang )

Chương 1: ĐỆ QUY
1. Tính giai thừa
Function giaithua(n:integer):longint;
Begin
If n=1 then giaithua:=1
Else Giaithua:=giaithua(n-1)*n;
End;
2. Tính Fibonacci
Function fibo(n:integer):longint;
Begin
If (n=1) or (n=2) then fibo:=1
Else fibo:=fibo(n-1)+fibo(n-2);
End;
3. Cho n nguyên dương, chuyển số n hệ 10 sang hệ 2 dùng đệ quy
Procedure nhiphan(n:integer);
Begin
If n=1 then write(n)
Else
Begin
Nhiphan(n div 2);
Write(n mod 2);
End;
End;
Begin
Nhiphan(4);
End.
4. Bài toán tháp Hà Nội
Procedure chuyendia(n:integer;a,b,c:char);
Begin
If n=1 then
Write(‘chuyen 1 dia tu ‘,a,’sang’,c)


Else
Begin
Chuyendia(n-1,a,c,b);
Chuyendia(1,a,b,c);
Chuyendia(n-1,b,a,c);
End;
End;
Begin
Chuyendia(2,’a’,’b’,’c’);
End;
5. Cho n nguyên dương, viết ra số ấy theo thứ tự đảo ngược bằng dùng đệ
quy
Procedure daonguoc(n:integer);
Begin
If n<10 then write(n)
Else
Begin
Write(n mod 10);
Daonguoc(n div 10);
End;
Begin
Daonguoc(4567);
End.
6. Liệt kê các hoán vị của N số tự nhiên
Var
A:array[1 100] of byte;
Cn:array[1 100] of boolean;
N,i:byte;
Procedure try(i);
Begin

For j:=1 to n do
If cn[j]=true then
Begin
X[i]:=j;
Cn[j]:=false;
If i=n then
In ket qua
Else
Try(i+1);
Cn[j]:=true;
End;
End;
Begin
Write(‘nhap n= ‘); readln(n);
For i:=1 to n do
Cn[i]:=true;
Try(1);
Readln;
end;
7. Cho 1 số n nguyên dương, liệt kê các số nhị phân có độ dài bằng n
Procedure try(i);
Begin
For j:= 0 to 1 do
Begin
X[i]:=j;
If i=n then
In kq
Else
Try(i+1);
End;

End;
8. Cho 1 số n nguyên dương, liệt kê các số nhị phân có độ dài bằng n sao
cho không có 2 số 1 liền kề nhau
Procedure try(i);
Begin
For j:=0 to 1 do
If not(j=1) and(x[i-1]=1))then
Begin
X[i]:=j;
If i= n then
In kq
Else
Try(i+1);
End;
End;
9. 8 quân hậu
Procedure try(i);
Begin
For j:=1 to 8 do
If cot[j] and cc[i-j] and cp[i+j] then
Begin
X[i]:=j;
Cot[j]:=false;
Cc[i-j]:=false;
Cp[i+j]:=false;
If i=8 then
In kq
Else
Try(i+1);
Cot[j]:=true;

Cc[i-j]:=true;
Cp[i+j]:=true;
End;
End;
10. Cho 5 kí tự A,B,C,D,E và số n nguyên dương, hãy liệt kê các xâu kí
tự có độ dài n được thành lập bởi 5 ký tự trên sao cho không có 2 ký tự
liên tiếp giống nhau.
Procedure try (i);
Begin
For j:=’a’ to ‘e’ do
If x[i-1] <> j then
Begin
X[i]:=j;
If i=n then
In kq
Else
Try(i+1);
End;
End;
11. Cho 1 ba lô có trọng lượng w và n đồ vật, mỗi đồ vật thứ i có giá trị
g[i] và trọng lượng là t[i] (mỗi đồ vật chỉ có 1 vật). Hãy tìm cách bỏ các
đồ vật vào trong ba lô sao cho gái trị của ba lô là lớn nhất.
Procedure try(i);
Begin
For j:=0 to 1 do
If (tl >= t[i]*J) then
Begin
X[i]:=j;
Giatri:=giatri+g[i]*j;
Tl:=tl-t[i]*j

If i=n then
Begin
If giatri>gttu then
Begin
Luux:=x;
Gttu:=giatri;
End;
End
Else
Try(i+1);
Giatri:=giatri-g[i]*j;
Tl:=tl+t[i]*j;
End;
End;
12. Cho số n nguyên dương, kiệt kê cá cách phân tích số n thành tổng các
số tự nhiên bé hơn n.
Procedure try(i);
Begin
For j:=1 to n- 1 do
If(tong+j<=n) and (j>=x[i-1]) then
Begin
X[i]:=j;
Tong:=tong+j;
If tong=n then
Inkq(i)
Else
If i<n) then try(i+1);
Tong:=tong-j;
End;
End;

13. Cho n ngôi nhà và m màu sơn liệt kê các cách sơn nhà.
Procedure try(i);
Begin
For j:= 1 to m do
Begin
X[i]:=j;
If i=n then
In kq
Else
Try(i+1);
End;
End;
14. Cho dãy gồm n số nguyên dương, liệt kê tất cả các dãy con có tổng
bằng s
Procedure try(i);
Begin
For j:= 1 to n do
If(tong+a[j]<=s) and cn[j] then
Begin
X[i]:=j;
Tong:=tong+a[j];
Cn[j]:=false
If tong=s then
In kq(i)
Else if (i<n) then
Try(i+1);
Tong:=tong-a[j];
Cn[j]:=true;
End;
End;

15. Cho dãy gồm n số nguyên dương, liệt kê theo thứ tự tất cả các dãy
con có tổng bằng s
Procedure try(i);
Begin
For j:= 1 to n do
If(tong+a[j]<=s) and (j>x[i-1]) then
Begin
X[i]:=j;
Tong:=tong+a[j];
If tong=s then
In kq(i)
Else if (i<n) then
Try(i+1);
Tong:=tong-a[j];
End;
End;
Chương 2: DANH SÁCH TUYẾN TÍNH
1. Tạo 1 danh sách có n nút( mỗi nút có một số nguyên)
Procedure taods(n:integer; var l:tronut);
Var p,q:tronut,i:integer;
Begin
For i:= 1 to n do
Begin
New(p);
Write(‘nhap thong tin cua nut:’);
Readln(p^.info);
P^.link=nil;
If l=nil then
L:=p
Else

Q^.link:=p;
Q:=p;
End;
End;
2. Duyệt danh sách
P:=l;
While p<>nil do
Begin
Xử lý nút p dạng trỏ;
P:=p^.link; {p^.link là nút sau nó};
End;
2.1 In danh sách
Procedure inds(l:tronut);
Var p:tronut;
Begin
P:=l;
While p<>nil do
Begin
Write(p^.info,’ ‘);
P:=p^.link; {p^.link là nút sau nó};
End;
End;
2.2. Tính tổng các nút
Function tongds(l:tronut):integer;
Var p:tronut; tong:integer;
Begin
P:=l; tong:=0
While p<>nil do
Begin
Tong:=tong+p^.ifno;

P:=p^.link; {p^.link là nút sau nó};
End;
Tongds:=tong;
End;
2.3. Tìm phần tử bé nhất ở trong danh sách
Function minds(l:tronut):integer;
Var p:tronut; min:integer;
Begin
P:=l^link;
Min:=l^.info;
While p<>nil do
Begin
If p^.info<min then
Min:=p^.info;
P:=p^.link;
End;
Minds:=min;
End;
2.4. Tìm nút có giá trị X
Function timds(l:tronut;x:integer):tronut;
Var p:tronut;
Begin
P:=l;
While (p<>nil) and(p^.info<>x) do
P:=p^.link;
Timds:=p;
End;
2.5. Tìm phần tử dương đầu tiên trong danh sách
Function timduongdautien(l:tronut):tronut;
Var p:tronut;

Begin
P:=l;
While(p<>nil)and(p^.info<0)do
P:=p^.link;
Timduongdautien:=p;
End;
2.6. Tìm phần tử dương bé nhất
Function timduongbenhat(l:tronut):integer;
Var p:tronut; min:integer;
Begin
P:=timduongdautien(l);
If p=nil then writeln(‘khong cos phan tu dung’)
Else
Begin
Min:=p^.info;p:=p^.link;
While(p<>nil) do
Begin
If (p^.info>0)and (p^.info<min)do
Min:=p^.info;
P:=p^.link;
end;
timduongbenhat:=min;
end;
end;
3. Bổ sung một nút vào cuối danh sách
Procedure bosung(var l:tronut;x:integer);
Var p,q:tronut;
Begin
New(p);
P^.info:=x;

P^.link:=nil;
If l=nil then l:=p
Else
Begin
Q:=l;
While q^.link<>nil do
Q:=q^.link;
Q^.link:=p;
End;
End;
4. Chèn 1 nút vào sau nút trỏ bởi M trong danh sách
Procedure chen(var l:tronut;x:integer;m:tronut);
Var p:tronut;
Begin
New(p);
P^.info:=x;
P^.link:=m^.link;
M^.link:=p;
End;
5. Xóa nút được trỏ bởi M
Procedure xoanut(var l:tronut;m:tronut);
Var p:tronut;
Begin
If l=m then l:=l^.link;
Else
Begin
P:=l;
While p^.link<>m do
P:=p^.link;
P^.link:=m^.link;

End;
Dispose(m);
End;
6. Nối danh sách l2 vào cuối danh sách l1;
Procedure noids(var l1:tronut;l2:tronut);
Var p:tronut;
Begin
If l1=nil then l1:=l2;
Else
Begin
P:=l1;
While p^.link<>nil do
P:=p^.link;
P^.link:=l2;
End;
End;
Chương II: NGĂN XẾP VÀ HÀNG ĐỢI
I. Ngăn xếp (stack)
1. Bổ sung:
Procedure bosung(x);
Begin
If top=n then write(‘Day stack day’);
Else
Begin
Top:=top+1;
Stack[top]:=x;
End;
End;

Procedure bosung(var top:tronut;x:integer);

Var p:tronut;
Begin
New(p);
P^.info:=x;
P^.link:=top;
Top:=p;
End;
2. Loại bỏ:
Function loaibo;
Begin
If top=0 then write(‘can stack roi’)
Else
Begin
Top:=top-1;
Loaibo:= Stack[top+1];
End;
End;

Function loaibo(var top:tronut);
Var p:tronut;x:integer;
Begin
X:=top^.info;
P:=top;
Top:=top^.link;
Dispose(p);
Loaibo:=x;
End;
3. Chuyển hệ 10 sang hệ 2
Procedure chuyen10_2(n:integer);
Begin

While n<>0 do
Begin
Bosung(top,n mod 2);
N:=n div 2;
End;
While top <> nil do
Write(loaibo(top));
End;
II. Hàng đợi (queue)
1. Bổ sung:
Procedure bosung(x);
Begin
If top=n then writeln(‘day’)
Else
Begin
Top:=top+1;
Queue[top]:=x;
If bottom = 0 then bottom:=1;
End;
End;

Procedure bosung(var top,bottom:tronut;x:integer);
Var p:tronut;
Begin
New(p);
P^.info:=x;
P^.link:=nil;
If top<>nil then top^.link:=p;
Top:=p;
If bottom=nil then bottom=top;

End;
2. Loại bỏ:
Function loaibo;
Begin
If bottom=0 then write(‘can’)
Else
Begin
Bottom:=bottom+1;
Loaibo:=queue[bottom-1];
If bottom>top then bottom:=top:=0;
End;
End;

Function loaibo (var top,bottom:tronut);
Begin
If bottom=nil then write(‘queue rong’)
Else
Begin
Loaibo:=bottom^.info;
P:=bottom;
Bottom:=bottom^.link;
Dispose(p);
If bottom=nil then top:=nil;
End;
End;

Chương 3: CẤU TRÚC CÂY
1. Khai báo cây
Type trocay=^cay;
Cay=record

Info:kiểu;
L,r:trocay;
End;
2. Duyệt cây theo thứ tự trước
Procedure thamgtp(root:trocay)
Begin
If root<>nil then
Begin
Thăm gốc;{xử lý nút gốc}
Thamgtp(root^.L);
Thamgtp(root^.r);
End;
End;
3. Duyệt theo thứ tự giữa (tgp)
Procedure thamtgp(root:trocay)
Begin
If root<>nil then
Begin
Thamtgp(root^.L);
Thăm gốc;{xử lý nút gốc}
Thamtgp(root^.r);
End;
End;
4. Thăm theo thứ tự sau (TPG);
Procedure thamtpg(root:trocay)
Begin
If root<>nil then
Begin
Thamtpg(root^.L);
Thamtpg(root^.r);

Thăm gốc;{xử lý nút gốc}
End;
End;
II. Cây nhị phân tìm kiếm
Function timkiem(root:trocay;x:integer):boolean;
Begin
If root=nil then timkiem:=false
Else
Begin
If root^.info=x then timkiem:=true
Else
If root^.info>x then timkiem:=timkiem(root^.l,x)
Else timkiem:=timkiem(root^.r,x);
End;
End;

Chương 4: ĐỒ THỊ
1. Duyệt rộng
Procedure duyetrong(u);
Begin
Bosung queue(u); chuatham[u]:=false;
While queue<>Æ do
Begin
V:=loaibo queue;
Tham v;{xử lý v}
For i:= 1 to n do
If chuatham[i] and (ke[v,i]=1) then
Begin
Bosung queue(i);
Chuatham[i]:=false;

Else;
End;
End;
2. Duyệt sâu:
Procedure duyetsau(u);
Begin
Bosung stack(u); chuatham[u]:=false;
While stack<>Æ do
Begin
V:=loaibo stack;
Tham v;
For i:= 1 to n do
If chuatham[i] and (ke[v,o]=1) then
Begin
Bosungstack(i);
Chuatham[i]:=false;
End;
End;
End;

Procedure duyetsaudq(u);
Begin
Tham u;
Chuatham[u]:=false;
For i:= 1 to n do
If chuatham[i] and(ke[u,i]=1) then
Duyetsaudq(i);
End;
3. Kiểm tra 2 đỉnh có liên thông không:
Function kiemtra2dinhlt(u,v:integer):boolean;

Begin
Duyetrong(u);
If chuatham[v] then kiemtra2dinhlt:=false
Else kiemtra2dinhlt:=true;
End;
4. Kiểm tra đồ thị có liên thông không
Function ktll:boolean;
Begin
Duyetrong(1);
I:=1;
While(i<=n) and not(chuatham[i]) do
I:=i+1;
If i>n then ktll:=true
Else ktll:=false;
End;
5. Đồ thị không liên thông, đếm số thành phần không liên thông
Function demtplt:byte;
Begin
Dem:=0;
For i:= 1 to n do
If chuatham[i] then
Begin
Dem:=dem+1;
Duyetrong(i);
End;
Demtplt:=dem;
End;
6. Tìm đường đi từ đỉnh này đến đỉnh kia
procedure timkiemrong(u);
Begin

Bosung(u);chuatham[u]:=false;
While queue<>Æ do
Begin
V:=loaibo;
If v=dich then xeit(‘tc’);
For i:= 1 to n do
If chuatham[i] and (ke[v,i]=1)then
Begin
Bosung(i);chuatham[i]:=false;truoc[i]:=v;
End;
End;
End;

×