2018 cs61b dis mid

https://sp18.datastructur.es/materials/exam/cs61b-sp18-mt1.pdf
1.Static Data
a.

        public static void main(String[] args) {
            String a = "alice";
            String b = "bob";
            String c = "carol";
            String d = "dan";
            String[][] twod = {{a, b}, {c, d}};
            String[] X = twod[1];
            printStringArray(X); //carol dan
            Dada.rs = X;
            String[] Y = Dada.rs;
            Y = new String[]{d, c};
            Dada.rs[1] = "eve";
            printStringArray(Dada.rs); //carol eve
            printStringArray(Y); //dan carol
            printStringArray(twod[0]); //alice bob
            printStringArray(twod[1]); //carol eve
        }

b.

        public static void main(String[] args) {
            String a = "alice";
            String b = "bob";
            String c = "carol";
            String d = "dan";
            String[][] twod = {{a, b}, {c, d}};
            Dada.rs = new String[]{"fritz", "gritz"};
            String[] X = twod[0];
            printStringArray(X); //alice bob
            fillOne(X[0]);//formal parameter
            printStringArray(X); // alice bob
            fillMany(X);
            printStringArray(X); //fritz gritz
        }

2What It Do
a.
eg:这是前二十个

对于 x>=1 这个是输出小于x的2的次方数,小于1就是无限循环函数。
b.这个题目意思画出图来表示列表的指向,如果对象没有被引用(就是与其他无关)称作garbage collected

3.KeyGate
从sb.unlock(k)看作第一个那么

1.UK
2.UK
3.UK
4.UK//sk继承于k
5.UK//sk继承于k
6.UF
7.Compile Error
8.Compile Error
9.UF
10.blank
11.UK//sk继承于k
12.UK//sk继承于k
13.Runtime Error//父类不能转换为子类

4.Sans
a.

        public static int[] sans(int[] x, int y) {
            int[] xclean = new int[x.length];
            int c = 0;
            for (int i = 0; i < x.length; i += 1) {
                if (x[i] != y) {
                    xclean[c] = x[i];
                    c += 1;
                }
            }
            int[] r = new int[c];
            System.arraycopy(xclean, 0, r, 0, c);
            return r;
        }

b.

        public static IntList ilsans(IntList x, int y) {
            if (x == null) {
                return null;
            }
            if (x.first == y) {
                return ilsans(x.rest, y);
            }
            return new IntList(x.first, ilsans(x.rest, y));
        }

c.

        public static IntList ilsans(IntList x, int y) {
            if (x == null) {
                return null;
            }
            x.rest = ilsans(x.rest, y);
            if (x.first == y) {
                return x.rest;
            }
            return x;
        }

d.

        public void testSans() { // TEST THE ARRAY VERSION OF SANS
            int[] x = new IntList.of(4, 5, 6);
            int y = 5
            int[] expected = new IntList.of(4, 6);
            int[] actual = sans(x, y);
            assertArrayEquals(expected, actual);
        }

        public void testIlsans() {
            IntList x = IntList.of(1, 2, 3);
            int y = 2;
            IntList expected = IntList.of(1, 3);
            IntList actual = Sans.ilsans(x, y);
            assertEquals(expected, actual);
            assertNotEquals(x, actual); // x should not have been altered 
        }

a.

        public class ArrayStack<Item> implements Stack<Item> {
            private Item[] items;
            private int size;
            public ArrayStack() { // initial array size is 8
                items = (Item[]) new Object[8];
                size = 0;
            }
            private void resize(int capacity) { // resizes array to given capacity
                Item[] temp = (Item[]) new Object[capacity];
                for (int i = 0 ;i < size; i++){
                    temp[i] = items[i];
                }
                items = temp;
            }
            public void push(Item x) {
                if (usageRatio() == 1) {
                    resize(size * 2);
                }
                items[size] = x;
                size++;
            }
            public Item pop() { // returns null if stack is empty
                if (size == 0) { return null; }
                if (usageRatio() < 0.25 && items.length > 8) { resize(items.length / 2);  }
                size = size - 1;
                Item returnItem = items[size];
                items[size] = null;
                return returnItem;

            }
            public int size() { return size; }
            private double usageRatio() { return ((double) size()) / items.length; }
        }

b.
先全部pop出来,然后如果和x不相等就push进去

    public default void purge(Item x) {
        if (size() == 0) {
            return;
        }
        Item top = pop();
        purge(x);
        if (!x.equals(top)) {
            push(top);
        }
    }

6.Combine
a.

        public class Combine {
            public static int combine(ComFunc f, int[] x) {
                if (x.length == 0) {
                    return 0;
                }
                if (x.length == 1) {
                    return x[0];
                }
                int t = f.apply(x[0], x[1]);
                return help(f, x, t, 2);
            }
            private static int help(ComFunc f, int[] x, int t, int k) {
                if (k == x.length) {
                    return t;
                }
                t = f.apply(x[k], t);
                return help(f, x, t, k + 1);
            }

b.

            public static int sumAll(int[] x) { // sumAll is not a member of Combine
                return combine(new Add(), x);
            }

7.The Downside of Default
a.

        default public void plusEquals(ListOfInts x) { // assume x is non-null
            if (x.size() != this.size()){ return; }
            for (int i = 0; i < this.length; i++) {
                this.set(i, x.get(i) + this.get(i));
            }
        }

b.

        public class DLListOfInts implements ListOfInts {
            public class IntNode {
                public int item;
                public IntNode next, prev;
            }
            public IntNode sentinel;
            public int size;
            @Override
            public void plusEquals(DLListOfInts x) {
                if (x.size() != this.size()){ return; }
                IntNode xp = x.sentinel.next;
                for (IntNode p = sentinel.next; p != sentinel; p = p.next) {
                    p.item = xp.item + p.item;
                    xp = xp.next;
                }
            }

c.
1.返回类型不一致,result是ListOfInts而函数类型是DLLisOfInts
2.破坏了原数组

posted @ 2022-06-10 14:40  天然气之子  阅读(35)  评论(0编辑  收藏  举报