Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 21 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
21
Dung lượng
752,08 KB
Nội dung
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 đệ qui Dr Dao Nam Anh Data Structure and Algorithm 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 Recursive Euclid Algorithm Tìm ước số chung lớn 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 p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 1272 = 216 + 192 p = 216, q = 192 environment gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm p = 1272, q = 216 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm p = 1272, q = 216 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } Data Structure and Algorithm p = 1272, q = 216 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) 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 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) 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 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) 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 environment p = 216, q = 192 environment p = 192, q = 24 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 24, q = environment gcd(1272, 216) gcd(24, 0) 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 environment p = 216, q = 192 environment p = 192, q = 24 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } p = 24, q = environment gcd(1272, 216) gcd(24, 0) 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 environment p = 216, q = 192 environment p = 192, q = 24 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 p = 24, q = environment gcd(1272, 216) gcd(24, 0) 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 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) 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 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 gcd(192, 24) 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 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) 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 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 gcd(216, 192) 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 environment gcd(1272, 216) 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]); 24 int q = Integer.parseInt(args[1]); System.out.println(gcd(p, q)); } 24 % java Euclid 1272 216 24 Data Structure and Algorithm 21 ... Algorithm in C (Parts 1-5 Bundle)- Third Edition by Robert Sedgewick, Addison-Wesley Data Structure and Algorithm Recursive Euclid Algorithm Tìm ước số chung lớn 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); }24 public class Euclid { public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q);... Integer.parseInt(args[0]); 24 int q = Integer.parseInt(args[1]); System.out.println(gcd(p, q)); } 24 % java Euclid 1272 216 24 Data Structure and Algorithm 21