Bài giảng Cấu trúc dữ liệu và giải thuật - Thực hiện thuật toán Euclid bằng đệ quy - Đào Nam Anh
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Cấu trúc dữ liệu và giải thuật - Thực hiện thuật toán Euclid bằng đệ quy - Đào Nam Anh", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Tài liệu đính kèm:
- bai_giang_cau_truc_du_lieu_va_giai_thuat_thuc_hien_thuat_toa.pdf
Nội dung text: Bài giảng Cấu trúc dữ liệu và giải thuật - Thực hiện thuật toán Euclid bằng đệ quy - Đào Nam Anh
- DATA STRUCTURE AND ALGORITHM Recursive Euclid Algorithm 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 Data Structure and Algorithm 1
- Resource - Reference Slides adapted from Robert Sedgewick, and Kevin Wayne. Major Reference: • Robert Sedgewick, and Kevin Wayne, “Algorithms” Princeton University, 2011, Addison Wesley • Algorithm in C (Parts 1-5 Bundle)- Third Edition by Robert Sedgewick, Addison-Wesley Data Structure and Algorithm 2
- Recursive Euclid Algorithm 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)); } } Data Structure and Algorithm 3
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 4
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 5
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 6
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { 1272 = 216 5 + 192 if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 7
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 8
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 9
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 10
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 11
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 12
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); p = 24, q = 0 } gcd(24, 0) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 13
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); p = 24, q = 0 } gcd(24, 0) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 14
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); p = 24, q = 0 } 24 gcd(24, 0) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm 15
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 Data Structure and Algorithm 16
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 192, q = 24 24 gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 Data Structure and Algorithm 17
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 Data Structure and Algorithm 18
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 216, q = 192 24 gcd(216, 192) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 Data Structure and Algorithm 19
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 Data Structure and Algorithm 20
- p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { 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[241]); System.out.println(gcd(p, q)); } 24 } % java Euclid 1272 216 24 Data Structure and Algorithm 21