אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2024 · אביב מועד ב · שאלה 12כל שאלות המבחן ←
רקורסיהאלגוריתמים רקורסיביים

נתונות שתי פעולות רקורסיביות הבאות:

language_variants: codeBlockOneTwo:

public static void one(Stack<Integer> s, int x){
    if(s.isEmpty())
        s.push(x);
    else
    {
        int temp=s.pop();
        one(s,x);
        s.push(temp);
    }
}

public static void two(Stack<Integer> s){
    if(!s.isEmpty())
    {
        int x = s.pop();
        two(s);
        one(s,x);
    }
}

codeBlockThree:

public static boolean three(Stack<Integer> s){
    if(s.isEmpty())
        return true;
    int x = s.pop();
    two(s);
    if(s.isEmpty())
        return true;
    int y = s.pop();
    if(x % y != 0)
        return false;
    two(s);
    return three(s);
}

namingConventionNote: In C# this paper capitalizes the method names One/Two/Three (matching the class-naming convention exactly) rather than the more common camelCase seen elsewhere in this exam's C# sections — still a casing-only delta, but flagged since it breaks the usual PascalCase-first-letter-only-differs-from-camelCase pattern seen in every other question (here the FULL identifier, including what would be a lowercase method name in idiomatic C#, is capitalized to literally match Java's method name capitalized).

סעיף א

מה יהיה התוכן של המחסנית s1 אחרי זימון הפעולה one(s1,10)/One(s1,10)? יש להראות מעקב אחרי ביצוע הפעולה one/One.

exampleFigure: s1: [3, -1, 7, 2]

confidence: MEDIUM — hand-drawn stack box, values read top-to-bottom, re-verify visually before use as a solve-oracle input.

סעיף ב

מה מבצעת הפעולה one(s,x)/One(s,x) עבור מחסנית s של מספרים שלמים ומספר שלם x?

סעיף ג

מה יהיה התוכן של המחסנית s1 אחרי זימון הפעולה two(s1)/Two(s1)? יש להראות מעקב אחרי ביצוע הפעולה two/Two. אין צורך במעקב אחרי הפעולה one/One.

סעיף ד

מה מבצעת הפעולה two(s)/Two(s) עבור מחסנית s של מספרים שלמים?

סעיף ה

מה תהיה תוצאת זימון הפעולה three(s2)/Three(s2)? יש להראות מעקב אחרי ביצוע הפעולה three/Three. אין צורך במעקב אחרי הפעולה two/Two.

exampleFigure: s2: [3, 6, 14, 2, 6, 3, 1]

שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי