Professional Documents
Culture Documents
Ni dung
Thut ton quy v hm quy l g?
Thut ton quy hot ng nh th no?
Mt s thut ton quy n gin
Droste
effect
Sierpinksi
triangle
quy V d
nh ngha s t nhin:
0 l s t nhin
n l s t nhin nu n-1 cng l s t nhin
Dy s:
Dy s l mt s
Dy s l mt s v sau l mt dy s
Mt s thut ng:
PHP = PHP: Hypertext Preprocessor
GNU = GNUs Not Unix
quy
Mt s thut ton rt kh tm ra li gii c th s dng k thut
quy gii quyt. V d: Bi ton thp H Ni
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
ti chnh n
xt v d tnh n! sau
S dng vng lp
S dng quy
int fact(int n) {
int result = 1;
for (int i=1;i<=n;i++)
result *= i;
return result;
}
int fact(int n) {
if (n == 0)
return 1;
else
return n * fact(n1);
}
Trng
Li gi quy hp c
8
s
quy tnh n!
120
fact(5)
5* fact(4)
24
4* fact(3)
fact(n):
if (n == 0) return 1;
else return n * fact(n-1);
3* fact(2)
2* fact(1)
1* fact(0)
1
1
9
Tnh n!
public class Factorial{
public int fact(int n){
if n == 0 return 1;
else return n*fact(n-1);
}
public static void main(){
System.out.println(n + ! = + fact(n));
}
}
10
Lp t kt tp chnh n
public class MyClass<E>{
private E element;
private MyClass<E> reference;//cha tham chiu c kiu MyClass
public MyClass(E item){
element = item;
reference = null
}
public setReference(MyClass<E> ref){
reference = ref;
}
public MyClass<E> getReference(){
return reference;
}
public void showElement(){
System.out.println(element.toString());
}
}
11
Lp t kt tp chnh n
B nh stack
B nh heap
aObj
A
reference
bObj
B
reference
Lp t kt tp chnh n
public class TestMyClass {
public static void main(String[] args){
MyClass<String> aObj = new MyClass<String>("A");
MyClass<String> bObj = new MyClass<String>("B");
aObj.setReference(bObj);
aObj.getReference().showElement();
bObj.setReference(aObj);
bObj.getReference().getReference().showElement();
bObj.setReference(bObj);
bObj.getReference().showElement();
bObj.getReference().setReference(
bObj.getReference().
getReference());
bObj.getReference().showElement();
}
}//Ch : Khng c li gi no trn l quy
Kt qu hin th l g?
13
2. MT S V D
14
Tnh s Fibonacci
Dy s Fibonacci: 1, 1, 2, 3, 5 ,8, 13
Tnh s Fibonacci th n:
Fn = 1 nu n 2
Fn = Fn-1 + Fn-2 nu n > 2
int fib(int n) {
if (n <= 2)
return 1;
else
return fib(n-1) + fib(n-2);
}
15
Li gi fib(3)
thc hin 3 ln!
fib(6)
fib(5)
fib(4)
fib(3)
fib(2)
fib(4)
fib(3)
fib(2)
fib(2)
fib(2)
fib(3)
fib(1)
fib(2)
fib(1)
fib(1)
16
17
Chuyn i c s
Chuyn mt gi tr nguyn dng N t h thp phn sang
h m khc vi c s 2 b 10
Cch thc hin:
Bc 1:Ly phn nguyn ca N(10) chia cho b, ta c thng l
T1 s d d1.
Bc 2: Nu T1 khc 0, Ly T1 chia tip cho b, ta c thng s
l T2 , s d l d2
(C lm nh vy cho ti bc th n, khi ta c Tn =0)
Bc n: Nu Tn-1 khc 0, ly Tn-1 chia cho b, ta c thng s l
Tn =0, s d l dn
Kt qu ta c s N(b) l s to bi cc s d (c vit theo
th t ngc li) trong cc bc trn
Phn nguyn ca N(10) = dndn-1d1 (b)
18
Chuyn i c s
public static void displayInBase(int n, int base) {
if (n > 0) {
displayInBase(n / base, base);
System.out.print(n % base);
}
}
V d 1:
n = 123,
base = 10
123/10 =12 123 % 10 = 3
12/10 = 1 12 % 10 = 2
1/10 = 0 1 % 10 = 1
Kt qu: 123
V d 2:
n = 123,
base = 8
123/8 = 15 123 % 8 = 3
15/8 = 1 15 % 8 = 7
1/8 = 0 1 % 8 = 1
Kt qu: 173
19
Bi ton thp H Ni
C 3 cc A, B, C.
Trn cc A c mt chng a, tm cch di chuyn
sang chng a khc.
Lut: a ln khng c t ln a nh
A
20
Bi ton thp H Ni
Trng hp c s l g?
A: 1 a
B: 0 a
Bc quy?
A: chuyn n-1 a trn sang cc khc
B: chuyn n-1 a di sang cc khc
21
Bi ton thp H Ni
public static void Towers(int numDisks, char A, char C,
char B) {
if (numDisks == 1) {
System.out.println("Move top disk from pole " + A
+ " to pole " + C);
} else {
Towers(numDisks 1, A, B, C);
// Gi quy ln
Towers(1, A, C, B);
// Gi quy ln 2
Towers(numDisks 1, B, C, A); // Gi quy ln
3
}
}
22
15
0
10
11
-4
-1
10
14
15
19
20
23
24
25