180 likes | 390 Views
תרגיל חזרה. מבחנים לדוגמא. שאלה 1. שאלה 1. public static int equalSum( int arr[]){ int i,sum1=0,sum2=0; for (i=0;i<arr.length;i++) sum1+=arr[i]; for (i=0;i<arr.length;i++){ sum1-=arr[i]; sum2+=arr[i]; if(sum2==sum1) return i; } return -1; }. שאלה 2.
E N D
תרגיל חזרה מבחנים לדוגמא
שאלה 1 public static int equalSum( int arr[]){ int i,sum1=0,sum2=0; for (i=0;i<arr.length;i++) sum1+=arr[i]; for (i=0;i<arr.length;i++){ sum1-=arr[i]; sum2+=arr[i]; if(sum2==sum1) return i; } return -1; }
שאלה 2 נתונה התכנית הבאה: publicstaticint what(int n) { int i; int sum=0; if(n == 0) return 1; for (i=0; i<n; i++) sum += what(i); return sum; } // what publicstaticvoid main(String[] args) { System.out.println( "what= " + what(4) ); } // main מהו הפלט של התכנית הנ"ל ? what = 8
שאלה 3 • כתוב פעולה public static String makeNewSentence(String str)אשר מקבלת strמחרוזת של טקסט ומחזירה מחרוזת חדשה המכילה את אותו טקסט כאשר: • לפני התו פסיק או נקודה אין רווח בכלל • לאחר התו פסיק או נקודה יש רווח אחד בלבד • לאחר הנקודה האחרונה שבסוף הטקסט אין רווח. • לדוגמא עבור המחרוזת הבאה : הפעולה תחזיר מחרוזת חדשה :
שאלה 3 public static String makeNewSentence(String s){ String fixStr = ""; for(int i = 0;i < s.length();i++){ if (s.charAt(i) == '.' || s.charAt(i) == ','){ // Remove spaces before point or comma for (int j = i-1; j >= 0 && s.charAt(j) == ' '; j--) fixStr = fixStr.substring(0,fixStr.length()-1); fixStr += s.charAt(i) + " "; // Remove redundant spaces after point or comma while ( i+1 < s.length() && s.charAt(i+1) == ' ') i++; } else fixStr += s.charAt(i); } // Remove redundant characters after the last point for (int i = fixStr.length()-1; i >=0 && fixStr.charAt(i) == ' '; i--) fixStr = fixStr.substring(0,fixStr.length()-1); return fixStr; }
שאלה 4 נתונה הפעולה הבאה: public static List<Integer> MaxSortedSubList(List<Integer> lst) הפעולה מקבלת כפרמטר רשימה של מספרים שלמים ומחזירה את התת-רשימה הרציפה הארוכה ביותר המסודרת בסדר עולה ממש. דוגמאות: עבור הרשימה: lst = 3, 1, 2, 3, 2, -3, -1, 2, 4, 7, הזימון MaxSortedSubList(lst) יחזיר את התת-רשימה בערך 5: [ -3, -1, 2, 4, 7 ] התת-רשימה העולה הארוכה ביותר, היא התת-רשימה המתחילה ב 3- ומסתיימת ב 7, ויש בה חמישה איברים. עבור הרשימה: lst = 10, 7, 5, 3, -2, -30, הזימון MaxSortedSubList(lst) יחזיר את התת-רשימה בערך 1, מכיוון שלא קיימת תת-רשימה עולה שאורכה 2 לפחות: [ 10 ] עבור הרשימה: lst = -4, 0, 1, 6, 12, 23, 90, הזימון MaxSortedSubList(lst) יחזיר את התת-רשימה בערך 7: [ -4, 0, 1, 6, 12, 23, 90 ] התת-רשימה העולה הארוכה ביותר היא כל הרשימה, ויש בה 7 איברים. השלימו בדפי התשובות את הקטעים החסרים המסומנים ב- ?? n ?? .
publicstatic List<Integer> MaxSortedSubList(List <Integer> lst) { Node<Integer> pos = lst.getFirst(); int x = ?? 1 ?? int len = 1; int maxLen = 0; Node<Integer> posStart = pos; Node<Integer> posStop = ?? 2 ?? Node<Integer> posStartMax = pos; Node<Integer> posStopMax = pos; pos = pos.getNext(); while (pos != null) { int y = pos.getData(); if (y >= x) { ?? 3 ?? posStop = pos; } // if else if?? 4 ?? { maxLen = len; len = 1; posStartMax = posStart; posStart = pos; ?? 5 ?? } // if x = y; ?? 6 ?? } // while if (len > maxLen) { maxLen = len; posStartMax = posStart; ?? 7 ?? } //if List<Integer> sortedList = new List<Integer>(); Node<Integer> p = sortedList.getFirst(); while (?? 8 ??) { p = sortedList.insert(p, ?? 9 ??); posStartMax = posStartMax.getNext(); } //while ?? 10 ?? } // MaxSortedList שאלה 4
publicstatic List<Integer> MaxSortedSubList(List <Integer> lst) { Node<Integer> pos = lst.getFirst(); int x = pos.getData(); int len = 1; int maxLen = 0; Node<Integer> posStart = pos; Node<Integer> posStop = pos; Node<Integer> posStartMax = pos; Node<Integer> posStopMax = pos; pos = pos.getNext(); while (pos != null) { int y = pos.getData(); if (y >= x) { len++; posStop = pos; } // if else if(len > maxLen) { maxLen = len; len = 1; posStartMax = posStart; posStart = pos; posStopMax = pos; } // if x = y; pos = pos.getNext(); } // while if (len > maxLen) { maxLen = len; posStartMax = posStart; posStopMax = pos; } //if List<Integer> sortedList = new List<Integer>(); Node<Integer> p = sortedList.getFirst(); while (posStartMax != posStopMax){ p = sortedList.insert(p, posStartMax.getData()); posStartMax = posStartMax.getNext(); } //while return sortedList; } // MaxSortedList שאלה 4
publicclass A { privateint n; privatechar ch; public A() { this.n=2; this.ch='G'; } public A(int n) { this.n=n; this.ch='M'; } public A(int n,char ch) { this.n=n; this.ch=ch; } public A(A other) { this.n=other.n; ch=other.ch; } publicint getN() { return this.n; } publicchar getCh() { return this.ch; } publicvoid inc() { this.n++; this.ch++; } public String toString() { String s=""; for(int i=0;i<this.n;i++) s=s+this.ch; return s; } } // class A שאלה 5 לפניך פרויקט ובו המחלקות A, B ו- Test5 publicclass B extends A { private A a; public B() { super(); a = new A(); } public B(int n) { super(n); a = new A(); } public B(A other) { super(); a = new A(other); } public B(A other,int n) { super(other); a = new A(n); } publicvoid inc() { this.a.inc(); } public A makeA() { returnnew A(one(this.a.getN(),this.getN()),one(this.a.getCh(),this.getCh())); } privateint one(int n,int m) { if(n>m) return n; return m; } privatechar one(char ch1,char ch2) { if(ch1<ch2) return ch1; return ch2; } public String toString() { returnthis.a.toString(); } } // class B publicclass Test5 { publicstaticvoid main(String[ ] args) { A a1 = new A(4,'E'); A a2 = new A(3); B b1 = new B(a1); a1.inc(); System.out.println(a1); System.out.println(a2); System.out.println(b1); B b2 = new B(b1,1); System.out.println(b2); A a3=b2.makeA(); System.out.println(a3); } // main } // class TEST5 FFFFF MMM EEEE M GG מהו הפלט של התכנית הנ"ל ?
שאלה 6 נתונה התכנית הבאה: publicstaticvoidwhat(int A[ ]) { int size = A.length; int distance = size / 2; while (distance > 0) { for (int i = distance; i < size; i++) { int j = i; int temp = A[i]; while (( j >= distance) && (A[j - distance] > temp)) { A[j] = A[j - distance]; j = j - distance; } // while A[ j ] = temp; } // for distance = ( distance == 2) ? 1 : distance / 2; }// while } // what publicstaticvoid main(String[] args) { int arr[ ] = {24, -19, 101,-101,16, 0, -24, -16, 16, 19, }; what(arr); for(int i=0; i < arr.length; i++) System .out.print(arr[i] + " "); System.out.println(); } // main מהו הפלט של התכנית הנ"ל ? מה יעודה של הפונקציה what ? -101 -24 -19 -16 0 16 16 19 24 101 הפונקציה what ממיינת את המערך
שאלה 7 מערך "סימטרי סכומי" הוא מערך המקיים את התנאי הזה: לכל זוגות איברים – האיבר האחד במקום ה- i מתחילת המערך והאיבר האחר במקום ה- i מסוף המערך יש אותו סכום. לדוגמה: במערך הבאה הסכום של כל זוג איברים על פי התנאי הוא 10. כתוב פעולה רקורסיביתpublic static boolean checkArray ( int arr[ ],int l,int h, int val) המקבלת כפרמטרים מערך שלמים arr , אינדקס תחתון l , אינדקס עליון h ומספר שלםval – הסכום המבוקש. הפעולה בודקת אם המערך arr הוא מערך "סימטרי סכומי". הנחות: אורך של מערך arr הוא זוגי , l < h .
שאלה 7 public static boolean checkArray ( int arr[ ],int l,int h, int val) { if(l>=h) return true; return (arr[l]+arr[h]==val) && checkArray(arr,l+1,h+1,val); }
שאלה 8 • נתונה הפעולה הבאה: • public static int MaxSubList(List<Character> lst, char x, char y) • הפעולה מקבלת רשימה של תווים ומחזירה את אורך התת-רשימה הגדולה ביותר שמתחילה בתו xומסתיימת בתו y. • אם לא קיימת תת-רשימה כזו, יש להחזיר 0. • דוגמאות עבור הרשימה:lst = 'a', 'd', 'z', 'a', 't', 't', 'a', 'y', 'w' • הזימון MaxSubList(lst,'z','a'), יחזיר את הערך 4: התת-רשימה lst = z, a, t, t, a • הזימון MaxSubList(lst,'w','w'), יחזיר את הערך 1. • הזימון MaxSubList(lst,'a','k'), יחזיר את הערך 0. לא קיימת תת-רשימה שמתחילה בתו a ומסתיימת בתו k.
publicstaticint MaxSubList(List<Character> lst, char x, char y) { Node<Character> pos = ??1 ?? boolean start = ?? 2 ?? int maxLen = 0; int curLen = ?? 3 ?? while (pos != null) { if (?? 4 ??) { ?? 5 ?? break; } pos = pos.getNext(); } // while if (x == y) ?? 6 ?? while (pos != null && ?? 7 ??) { if (pos.getData() == y) { maxLen += ?? 8 ?? curLen = 0; } ?? 9 ?? pos = pos.getNext(); } // while ?? 10 ?? } //MaxSubList
publicstaticint MaxSubList(List<Character> lst, char x, char y) { Node<Character> pos = lst.getFirst(); boolean start = false; int maxLen = 0; int curLen = 0; while (pos != null) { if (pos.getData() == x) { true; break; } pos = pos.getNext(); } // while if (x == y) curLen = 1; while (pos != null && start) { if (pos.getData() == y) { maxLen += curLen; curLen = 0; } curLen++; pos = pos.getNext(); } // while return maxLen; } //MaxSubList
publicclass B { privatestaticintnumB =0; privateint m1; privateint m2; public B(int m1, int m2) { this.m1 = m1; this.m2 = m2; this.numB++; System.out.println("B(" +m1+ "," + m2 +"),#" + numB); } } // class B שאלה 9 לפניך פרויקט ובו המחלקות : B,A,D ו - Test5 publicclass A { privatestaticintnumA = 0; private A a; private B b; public A(A a, B b) { this.a = a; this.b = b; numA++; System.out.println("A Constructor, #" + numA); } } // class A publicclass D extends B { privatestaticintnumD = 0; privatedouble d; public D(double d,int x) { super(x,x); this.d = d; numD++; System.out.println("D(" + d + "," + x + "),#" + numD); } public D(double d,int x, int y) { super(x,y); this.d=d; numD++; System.out.println("D(" + d + "," + x + "),#" + numD); } } // class D publicclass Test5 { publicstaticvoid main(String[] args) { B w1 = new B(2,3); B w2 = new D(1.5, 6); B w3 = new D(2.3, 8, 9); A w4 = new A(null, w1); A w5 = new A(w4, w3); } } // class Test5 מהו הפלט לאחר ביצועהפעולה main במחלקה Test5?
שאלה 9 B(2,3),#1 B(6,6),#2 D(1.5,6),#1 B(8,9),#3 D(2.3,8),#2 A Constructor, #1 A Constructor, #2