You are on page 1of 6

Ni dung

TRNG I HC BCH KHOA H NI


VIN CNG NGH THNG TIN V TRUYN THNG

5.1. Thut ton s hc


5.2. Thut ton v dy
5.3. Thut ton quy

TIN HC I CNG
Bi 5. Mt s thut ton thng dng

5.3. Thut ton s hc

Bi ton s nguyn t
Cho mt s nguyn dng p. Lm th no bit
c p c phi s nguyn t hay khng?

Cc bi ton v s hc
Xc nh mt s nguyn c phi l s nguyn
t/hp s hay khng
Tm USCLN, BSCNN ca 2 s nguyn
..

Input: p nguyn dng


Output: kt lun v tnh nguyn t ca p

tng?
p = 1? Khng phi s nguyn t
p > 1?
Kim tra t 2 n p-1 c phi l c s ca p khng
Nu c th kt lun p khng l s nguyn t, ngc
li khng c s no th kt lun p l s nguyn t

Bi ton s nguyn t (2)

Tm UCLN, BCNN ca hai s

Nhp p
if p=1 then begin
Xut: p khng nguyn t;
Dng thut ton;
end
flag := TRUE
for k:=2 to p-1 do
if (k l c s ca p) then begin
flag:=FALSE;
break; { ngt vng lp FOR }
end
if flag=TRUE then
Xut: p l s nguyn t
else
Xut: p khng l s nguyn t

B1 : Nhp 2 s a, b
B2 : x a, y b
B3 : Nu y # 0, sang B4
Ngc li, UCLN x, BCNN (a*b)/ UCLN. Kt thc
B4 : r x mod y
xy
yr
Quay li B3.

V d

V d

1. Tm tt c cc c s ca mt s nguyn dng N
tng : Duyt ln lt cc gi tr t 1 ti N v in ra
nu gi tr l c s ca N.
Gii thut
B1 : Nhp N
B2 : Gn i 1
B3 : Nu i <= N ti B4
Ngc li, ti B6
B4 : Nu N chia ht cho i, xut i.
B5 : Tng i thm 1 n v. Quay li B3
B6 : Kt thc

2. Tm tt c cc c s l ca mt s nguyn dng N
tng : Duyt ln lt cc s l t 1 ti N v in ra nu
gi tr l c s ca N.
Gii thut
B1 : Nhp N
B2 : Gn i 1
B3 : Nu i <= N ti B4
Ngc li, ti B6
B4 : Nu N chia ht cho i, xut i.
B5 : Tng i thm 2 n v. Quay li B3
B6 : Kt thc
7

V d

V d

3. Kim tra tnh hon ho ca s nguyn dng N.


B1 : Nhp N
B2 : Gn i 1, TongUoc = 0,
B3 : Nu i < N ti B4
Ngc li, ti B6
B4 : Nu N chia ht cho i, gn TongUoc TongUoc + i
B5 : Tng i thm 1 n v. Quay li B3
B6 : Nu TongUoc = N, kt lun N l s hon ho.
Nu TongUoc < N, kt lun N l s khng y .
Nu TongUoc > N, kt lun N l s giu c
B7 : Kt thc

4. Hin th tt c cc s hon ho trong on [1,10000]

10

Ni dung

5.4. Thut ton v dy

5.1. Cc cu trc c bn trong lp trnh


5.2. Gi m (pseudocode)
5.3. Thut ton s hc
5.4. Thut ton v dy
5.5. Thut ton quy

Lm vic vi mt dy s
Cc bi ton in hnh
Tm s ln nht, nh nht trong dy
Kim tra dy c phi l dy tng hoc dy
gim
Sp xp dy tng dn hoc gim dn
Tm trong dy c phn t no bng mt gi tr
cho trc
Tnh trung bnh cng ca dy

11

12

Bi tp

Bi 1
tng : Gi s GTT ln nht ca dy
MaxAbs l |a1|. Duyt cc phn t ai khc trong
dy, nu c GTT ln hn MaxAbs th gn
MaxAbs = |ai|
Gii thut:
B1 : Nhp n v dy s {an}
B2 : Gn MaxAbs |a1|, i2
B3 : Nu i <= n ti B4
Ngc li, MaxAbs l gi tr cn tm. Ti B6.
B4 : Nu |ai| > MaxAbs, gn MaxAbs|ai|
B5 : i i +1. Quay li B3
B6 : Xut MaxAbs. Kt thc

Bi 1. Xy dng thut ton tm phn t c gi tr truyt


i ln nht trong dy gm n phn t.
Bi 2. Xy dng thut ton tm tng ca cc s chn v
tng ca cc s l trong dy gm n phn t c nhp
vo t bn phm.
Bi 3. Xy dng thut ton kim tra xem mt dy s
gm n phn t c nhp vo t bn phm c phi l
dy s tng (hoc gim) khng.
Bi 4. Xy dng thut ton tnh trung bnh cng ca cc
s dng trong dy gm n s c nhp vo t bn
phm.

13

14

Bi 2

Bi 5

tng : Gi tng cc s chn v tng cc s l ln lt l


TongChan v TongLe. Duyt ln lt cc phn t ca dy, xt tnh
chn l v cng dn vo tng tng ng.

Tm s Fibonacci th N
Thut gii
B1 : Nhp N
B2 : Gn i 1, FN = 2, Ftruoc = 1
B3 : Nu i < N ti B4.
Ngc li, xut FN. Kt thc.
B4 : Gn Fsau FN + Ftruoc
Gn Ftruoc FN
Gn FN Fsau
Gn i i + 1
Quay li B3

B1 : Nhp n v dy {an}
B2 : Gn TongChan 0, TongLe 0, i 1
B3 : Nu i <= n ti B4
Ngc li ti B6
B4 : Nu ai chn, gn TongChan TongChan + ai
Ngc li, gn TongLe TongLe + ai
B5 : Tng i thm 1 n v. Quay li B3
B6 : Xut TongChan, TongLe
B7 : Kt thc

15

16

Ni dung

5.5. Thut ton quy

5.1. Cc cu trc c bn trong lp trnh


5.2. Gi m (pseudocode)
5.3. Thut ton s hc
5.4. Thut ton v dy
5.5. Thut ton quy

Vi bi ton c th c phn tch v a


ti vic gii mt bi ton cng loi nhng
cp thp hn
ln d liu nhp nh hn
gi tr cn tnh ton nh hn

T thc hin li thut ton


V d:
Giai tha: n! = (n-1)! * n
Dy s Fibonacci: 0, 1, 1, 2, 3, 5, 8...
F(n) = F(n-1) + F(n-2)
17

18

5.5. Thut ton quy (2)

V d

xy dng thut ton quy, cn xc nh:

Tnh giai tha ca n:

Trng hp c bn: (Cc) trng hp khng cn


thc hin li thut ton.
Phn tng qut: C yu cu gi quy
Cn xc nh nguyn l a trng hp tng qut
v trng hp c bn
m bo tnh dng ca gii thut quy - chc
chn t trng hp tng qut s n c trng
hp c bn

Trng hp c bn: 0! = 1
Trng hp tng qut: n! = (n-1)! * n

Xy dng dy Fibonacci
Trng hp c bn: F(0) = F(1) = 1
Trng hp tng qut: F(n) = F(n-1) + F(n-2)

19

20

Tnh giai tha - Thut ton quy

Bi tp

Input: s t nhin n
Output: GT(n)=n!
Thut gii:
Nhp n
GT:=1;
if n>0 then
GT := GT(n-1)*n;
Xut GT

Xy dng thut ton cho bi ton tm s


Fibonacci F(n)

21

22

Thut gii heuristic


Dng mo
p dng vi nhng bi ton
Cha tm c thut ton v khng bit c
tn ti thut ton khng
C thut ton nhng thi gian tnh ton qu
lu hoc iu kin ca thut ton kh p
ng

23