Data Structure and Algorithm 1CẤU TRÚC DỮ LIỆU VÀ GIẢI THUẬT Thực hiệu thuật toán Euclid bằng đệ qui Dr.. Data Structure and Algorithm 2and Kevin Wayne.. Major Reference: Princeton Unive
Trang 1Data Structure and Algorithm 1
CẤU TRÚC DỮ LIỆU VÀ GIẢI THUẬT
Thực hiệu thuật toán Euclid bằng đệ qui
Dr Dao Nam Anh
Trang 2Data Structure and Algorithm 2
and Kevin Wayne
Major Reference:
Princeton University, 2011, Addison Wesley
• Algorithm in C (Parts 1-5 Bundle)- Third Edition by
Robert Sedgewick, Addison-Wesley
Trang 3Data Structure and Algorithm 3
Tìm ước số chung lớn nhất của hai số nguyên
public class Euclid {
public static int gcd (int p , int q ) {
if ( q == 0 ) return p ; else return gcd ( q , p % q );
}
public static void main ( String [] args ) {
int p = Integer parseInt ( args [ 0 ]);
int q = Integer parseInt ( args [ 1 ]);
System out println ( gcd ( p , q ));
} }
Trang 4Data Structure and Algorithm 4
}
Trang 5Data Structure and Algorithm 5
if (q == 0 ) return p;
else return gcd (q, p % q);
}
Trang 6Data Structure and Algorithm 6
}
Trang 7Data Structure and Algorithm 7
Trang 8Data Structure and Algorithm 8
Trang 9Data Structure and Algorithm 9
Trang 10Data Structure and Algorithm 10
p = 192, q = 24
p = 216, q = 192
environment
Trang 11Data Structure and Algorithm 11
p = 192, q = 24
p = 216, q = 192
environment
Trang 12Data Structure and Algorithm 12
p = 192, q = 24
p = 216, q = 192
environment
Trang 13Data Structure and Algorithm 13
static int gcd (int p, int q) {
Trang 14Data Structure and Algorithm 14
static int gcd (int p, int q) {
Trang 15Data Structure and Algorithm 15
static int gcd (int p, int q) {
Trang 16Data Structure and Algorithm 16
24
p = 192, q = 24
p = 216, q = 192
environment
Trang 17Data Structure and Algorithm 17
Trang 18Data Structure and Algorithm 18
Trang 19Data Structure and Algorithm 19
Trang 20Data Structure and Algorithm 20
Trang 21Data Structure and Algorithm 21
if (q == 0 ) return p;
else return gcd (q, p % q);
}
24
public class Euclid {
public static int gcd (int p , int q ) {
if ( q == 0 ) return p ;
else return gcd ( q , p % q );
}
public static void main ( String [] args ) {
int p = Integer parseInt ( args [ 0 ]);
int q = Integer parseInt ( args [ 1 ]);
System out println ( gcd ( p , q ));