Problem:
We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication).
bunnyEars(0) → 0
bunnyEars(1) → 2
bunnyEars(2) → 4
Solution:
public int bunnyEars(int bunnies) { if (bunnies == 0) return 0; return 2 + bunnyEars(bunnies-1); }
public int bunnyEars(int bunnies) {
ReplyDeletereturn 2 * bunnies;
}
"without loops or multiplication" (c)
Deletepublic int bunnyEars(int bunnies) {
ReplyDeletereturn (bunnies ==0)? 0 : 2+bunnyEars(bunnies-1);
}
123
ReplyDelete