Tài liệu CAU TRUC & GIAI THUAT - Pdf 82

I.Viết các thủ tục và hàm đệ quy, ko đệ quy.
1. Tính Sn = 1 + 2 + 3 +…..+ n.
Function tong 1 (n : integer) : integer;
Var s, i : Integer;
Begin
S:= 0;
For i:=1 to n do S:= S+i;
Tong 1:= S;
END;
• Đệ quy
Function tong 1 (n : integer) : integer;
Begin
If n = 1 then tong 1:= 1;
Else tong1:= n + tong1 (n – 1);
End;
2. Tính Sn = 1
2 +
2
2
+ 3
2
+……+n
2
Function tong 2 (n : integer) : word;
Var i: interger; s: word;
Begin
S:= 0;
If n > 0 then
For i:= 1 to n do S:= S +i*i
Tong 2 := S;
End;

Nhiphan:= S;
End;
* Procedure Nhiphan (n:integer);
Var
Begin
S := rong;
Repeat
X := n mod 2;
SPUSH (S, n, t, X);
n:= n div 2;
Until n:= 0 ;
While s <> rong do
Wrrite ( SPOP (S, nt));
End;
• De quy
Function Nhiphan (n : integer) : string;
Begin
If n >= 2 then
Nhiphan (n div 2);
Write ( n mod 2);
End;
5. Hien thi so dao nguoc
Procedure daonguoc ( n : integer);
Var m:integer;
Begin
Repeat
m:=n mod 10;
Write (m);
n:= n div 10;
Until n:= 0;

While ( a < n div 2) and ( n mod 2 <> 0)
Do a:= a + 1;
If a > n div 2 then nguyento := true;
Else nguyento := False;
End;
8. Đếm số chữ số trong số nguyên n.
* Function Dem(n:integer):integer;
Begin
If n<>0 then Dem:=Dem(n div 10) +1
Else Dem:=0;
End;
* Không Đệ Quy:
Function Dem(n:integer): integer;
Var a: word;
Begin
a:=0;
Repeat
a:=a+1;
n div 10;
Until n=0;
Dem:=a;
End;
9. Đếm số lẻ trong số nguyên n.
* Function Dem(n:integer):integer;
Begin
While n<>0 the do
Begin
If n mod 2 <>0 then Dem:=1
Else Dem:=Dem(n div 10);
End;

P:=Botton;
Botton:=Botton.Link;
If (Botton=nil) then Top=nil;
Dispose(P);
QPOP:=X;
End;
III. DANH SÁCH MÓC NỐI ĐƠN
1. Bổ sung một nút vào danh sách
a, Bổ sung dầu dang sách
Procedure bosung (var L : Tro; X : integer);
Var P: tro;
Begin
New (p);
P^. gtri := X;
P^. Next := L;
L:= P;
End;
b, Bổ sung cuối dang sách
Procedure bosung (var L : Tro; X : integer);
Var P: tro;
Begin
New (p);
P^. gtri := X;
P^. Next := nil;
If L := nil then L:= P;
Else
Begin
T:= L;
While t^.next <> nil do T:=T^.next
T^.next := P;


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status