Statistics:
mean 22.974 (896.000/39) stddev 4.780 median 24.000 midrange 19.000-26.000 # avg 1 5.31 / 7 2 4.13 / 8 3 13.54 / 15
[7 pts] Draw a recursion tree representing the recursive calls made to f in the course of computing the value of f(11) using the following method.
public static void f(int n) {
if(n <= 1) {
return 1;
} else if(n % 2 == 0) {
return 2 * f(n / 2);
} else {
return f((n - 1) / 2) + f((n + 1) / 2);
}
}
[8 pts] Consider the following method.
public static void run() {
int n = IO.readInt();
for(int i = n; i > 0; i /= 2) {
IO.print(i % 2);
}
}
[15 pts] Define a class PigPen for tracking the number of pigs in in a pen. It should support the following methods.
public class PigPenTest {
public static void run() {
PigPen pen = new PigPen(2);
pen.pigExits();
IO.println(pen.isEmpty()); // prints ``false''
pen.pigExits();
IO.println(pen.isEmpty()); // prints ``true''
pen.pigEnters();
IO.println(pen.isEmpty()); // prints ``false''
}
}
11
/ \
5 6
/ \ |
2 3 3
| / \ / \
1 1 2 1 2
| |
1 1
public class PigPen {
private int count;
public PigPen(int pigs) {
count = pigs;
}
public boolean isEmpty() {
return count == 0;
}
public void pigEnters() {
count++;
}
public void pigExits() {
count--;
}
}