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)
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;
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
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;
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;
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;
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);
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;
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);
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;
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;