Você está na página 1de 13

*

Trn Minh Thi


minhthai@itc.edu.vn

1
*
*Mt ham c goi co tinh qui nu trong
thn cua ham o co lnh goi lai chinh no mt
cach tng minh hay tim n.
*Phn loai qui
* qui tuyn tinh.
* qui nhi phn.
* qui phi tuyn.
* qui h tng.
2
*
Trong thn ham co duy nht mt li goi ham goi lai
chinh no mt cach tng minh.
<Kiu d liu ham> TenHam (<danh sach tham s>)
{
if (iu kin dng)
{
...
//Tra v gia tri hay kt thuc cng vic
}
//Thc hin mt s cng vic (nu co)
. . . TenHam (<danh sach tham s>);
//Thc hin mt s cng vic (nu co)
}
3
Vi du: Tinh S (n) 1 2 3 n
- iu kin dng: S(0) = 0.
- Qui tc (cng thc) tinh: S(n) = S(n-1) + n.
int TongS (int n)
{
if(n==0)
return 0;
return ( TongS(n-1) + n );
}
4
*
1. Tnh n!
2. In ra cc c s ca s nguyn dng
3. m s lng c s ca s nguyn dng
4. Tm c s chung ln nht ca 2 s nguyn
dng
5. Kim tra s nguyn dng n c phi l s nguyn
t?
6. Nhp vo mng 1 chiu s nguyn a, kch thc n
7. Xut mng 1 chiu s nguyn a, kch thc n
8. Tm phn t c gi tr x trong mng s nguyn a,
kch thc n
5
*
Trong thn cua ham co hai li goi ham goi lai chinh no mt cach tng
minh.
<Kiu d liu ham> TenHam (<danh sach tham s>)
{
if (iu kin dng)
{
...
//Tra v gia tri hay kt thuc cng vic
}
//Thc hin mt s cng vic (nu co)
. . .TenHam (<danh sach tham s>); //Giai quyt vn nho hn
//Thc hin mt s cng vic (nu co)
. . . TenHam (<danh sach tham s>); //Giai quyt vn con lai
//Thc hin mt s cng vic (nu co)
} 6
Vi du: Tinh s hang th n cua day Fibonaci
c inh nghia nh sau:
f1 = f0 =1 ;
fn = fn-1 + fn-2 ; (n>1)
iu kin dng: f(0) = f(1) = 1.
long Fibonaci (int n)
{
if(n==0 || n==1)
return 1;
return Fibonaci(n-1) + Fibonaci(n-2);
}
7
*
* Trong thn cua ham co li goi ham goi lai chinh no c t bn trong vong
lp.
<Kiu d liu ham> TenHam (<danh sach tham s>)
{
for (int i = 1; i<=n; i++)
{ //Thc hin mt s cng vic (nu co)
if (iu kin dng)
{ ...
//Tra v gia tri hay kt thuc cng vic
}
else
{ //Thc hin mt s cng vic (nu co)
TenHam (<danh sach tham s>);
}
}
}
8
Vi du: Tinh s hang th n cua day {Xn} c inh nghia
nh sau:
X0 =1 ;
Xn = n2X0 + (n-1)2X1 + + 12Xn-1 ; (n1)

iu kin dng:X(0) = 1.
long TinhXn (int n)
{
if(n==0)
return 1;
long s = 0;
for (int i=1; i<=n; i++)
s = s + i * i * TinhXn(n-i);
return s;
} 9
*
*Trong thn cua ham nay co li goi ham
n ham kia va trong thn cua ham kia co
li goi ham ti ham nay.

g()
f()

f()
f() g()
h()

10
<Kiu d liu ham> TenHam2 (<danh sach tham s>);
<Kiu d liu ham> TenHam1 (<danh sach tham s>)
{
//Thc hin mt s cng vic (nu co)
TenHam2 (<danh sach tham s>);
//Thc hin mt s cng vic (nu co)
}
<Kiu d liu ham> TenHam2 (<danh sach tham s>)
{
//Thc hin mt s cng vic (nu co)
TenHam1 (<danh sach tham s>);
//Thc hin mt s cng vic (nu co)
}

11
Vi du: Tinh s hang th n cua hai day {Xn}, {Yn} c inh nghia nh sau:
X0 =Y0 =1 ;
Xn = Xn-1 + Yn-1; (n>0)
Yn = n2Xn-1 + Yn-1; (n>0)
- iu kin dng:X(0) = Y(0) = 1.

long TinhYn(int n);


long TinhXn (int n)
{
if(n==0)
return 1;
return TinhXn(n-1) + TinhYn(n-1);
}

long TinhYn (int n)


{
if(n==0)
return 1;
return n*n*TinhXn(n-1) + TinhYn(n-1);
}

12
*
*V d tnh n! vi n=5

main() GiaiThua(5) GiaiThua(4) GiaiThua(3) GiaiThua(2) GiaiThua(1)


5 4 3 2 1
n 5 n 5 n 4 n 3 n 2 n 1
120 24 6 2 1

13