Recursive addition public static int addint a int
Recursive addition public static int add(int a, int b) { if(b == 0) מקרי בסיס return a; else if(b < 0) return add(a-1, b+1); else return add(a+1, b-1); } public static void main(String[] args) { int x=reader. next. Int(); // 5 int y=reader. next. Int(); // 2 System. out. println(add(x, y)); } // main a b 5 2 6 1 7 0 a b 5 -2 4 -1 3 0 קריאה רקורסיבית עם בעיה מוקטנת 5
Recursive subtraction public static int sub(int a, int b) { if(b == 0) מקרי בסיס return a; else if(b < 0) return sub(a+1, b+1); else return sub(a-1, b-1); } public static void main(String[] args) { int x=reader. next. Int(); // 5 int y=reader. next. Int(); // 2 System. out. println(sub(x, y)); } //main a b 5 2 4 1 3 0 a b 5 -2 6 -1 7 0 קריאה רקורסיבית עם בעיה מוקטנת 6
חילוק רקורסיבי public static int divide(int a, int b) { if(b == 0) מקרי בסיס return 0; else if(a < b) return 0; else if(b == 1) return a; else return add(1, divide(sub(a, b)); } Divide(6, 3) 2 6 b 3 add(1, divide(sub(a, b)); add(1, 1); =2 1 Divide(3, 3) a 3 b 3 add(1, divide(sub(a, b)); add(1, 0); =1 0 קריאה רקורסיבית עם בעיה מוקטנת a Divide(0, 3) a 0 b 3
חילוק רקורסיבי public static int divide(int a, int b) { if(b == 0) מקרי בסיס return 0; else if(a < b) return 0; else if(b == 1) return a; else return add(1, divide(sub(a, b)); } Divide(21, 10) 2 21 b 10 add(1, divide(sub(a, b)); add(1, 1); =2 1 Divide(11, 10) a 11 b 10 add(1, divide(sub(a, b)); add(1, 0); =1 0 קריאה רקורסיבית עם בעיה מוקטנת a Divide(1, 10) a 1 b 10
רקורסיה הדדית public static boolean is. Even(int n) { boolean ans; if (n == 0) ? is. Even(4) ans = true; else ans = is. Odd(n - 1); return ans; } public static boolean is. Odd(int n) { boolean ans; if (n == 0) ans = false; else ans = is. Even(n - 1); return ans; } איזה קריאות יהיו לנו עבור
דוגמת הרצה • sum. Digits(234557) public static int sum. Digits(int num){ if (num < 10){ return num; } return sum. Digits(num / 10) + (num % 10); } 26 sum. Digits(23455) +7 sum. Digits(2345) 19 +5 sum. Digits(234) 14 +5 sum. Digits(23) sum. Digits(2) 9 +4 5 +3
תרגיל ? • מה יודפס עבור כל אחת מן התוכניות הבאות public static void print 1(int n){ if (n == 0){ System. out. println(n); } else{ print 1(n-1); System. out. println(n); } } public static void print 2(int n){ if (n == 0){ System. out. println(n); } else{ System. out. println(n); print 2(n-1); } } public static void main(String[] args){ print 1(4); } print 2(4); }
פתרון ? • מה יודפס עבור כל אחת מן התוכניות הבאות public static void print 1(int n){ if (n == 0){ System. out. println(n); } else{ print 1(n-1); System. out. println(n); } } public static void print 2(int n){ if (n == 0){ System. out. println(n); } else{ System. out. println(n); print 2(n-1); } } public static void main(String[] args){ print 1(4); } print 2(4); } print 1: 0 1 2 3 4 print 2: 4 3 2 1 0
מעקב אחר פונקציה רקורסיבית - פתרון : ? מה הפונקציה הבאה מחשבת public static int mystery(int a, int b) { int ans; if (b == 0) { mystery(2, 5) mystery(4, 2)*2 ans = 1; mystery(16, 1) } else if (b % 2 == 0) { mystery(256, 0)*16 ans = mystery(a*a, b/2); } else { Returns: 1*16*2=32 ans = mystery(a*a, b/2) * a; 32=2^5 } return ans; } בן גוריון , מבוא למדעי המחשב 17
פתרון 2013 ' מועד א What(0)=1 What(1)=for[0. . 1]: What(0)=1 What(2)=for [0. . 2]: what(0)+what(1) = 2 What(3)=for [0. . 3]: what(0)+what(1)+what(2) = 4 what = 8 What(4)=for [0. . 4]: what(0)+what(1)+what(2)+what(3) = 8
- Slides: 19