The problem link is this:
CodingBat Java Recursion-2 groupSum5
Now, I've never done this kind of recursion problem before, the kind that has more than 1 argument in the method. Just need a complete explanation on how to go about this. Any help is greatly appreciated.
2 replies to this topic
#1
Posted 21 April 2011 - 07:21 PM
|
|
|
#2
Posted 22 April 2011 - 06:31 AM
Giving that this is a public challenge, part of the point is to try to solve it yourself.
In general, you want to think about how you can change each of the three arguments to indicate progress.
As an initial start, however, I'd simply look at the four sample calls and make sure you understand why each of them produces the results indicated, given the problem description.
In general, you want to think about how you can change each of the three arguments to indicate progress.
As an initial start, however, I'd simply look at the four sample calls and make sure you understand why each of them produces the results indicated, given the problem description.
#3
Posted 22 April 2011 - 10:40 AM
I guess I should've also posted what I've done so far so people don't think I'm just asking for the answer straight up. I know what I'm supposed to do and how that problem works, but I don't know how I'm supposed to do the recursion part. I'll post what I have so far later, when I get home.
EDIT: Here's what I have so far:
I know I didn't type in the return value yet. I just need help figuring out how to do the recursion so that it will reach termination instead of recurring infinitely.
EDIT: Here's what I have so far:
public boolean groupSum5(int start, int[] nums, int target) {
int c = 0;
int sum = 0;
int start1 = nums[0];
int target1 = nums[nums.length - 1];
if (start > 0)
return false;
else if (nums[c] % 5 == 0)
{
sum += nums[c];
c++;
}
groupSum5(start1, nums, target1);
}
I know I didn't type in the return value yet. I just need help figuring out how to do the recursion so that it will reach termination instead of recurring infinitely.
Edited by bloodchains, 22 April 2011 - 07:14 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









