To me, if the question implies an ordering to the process, it's better if the code mimics that process. Start by subtracting as many large chocolate bars as possible from the total, then consider whether the small ones are enough to fit the remainder. If you separate the two steps like that, it makes it easier to read what's happening than if you calculate a few numbers at the start, then compare them and spit out a result.
In this case, you want to subtract either
a) all the large chocolate
b) as many large chocolates as fit in the total
Depends which is smaller.
-- a is easy to calculate, it's just 'big'
-- b is total/5
So,
Code:
// remove as many big chocolates as possible, while leaving total positive
if (total/5 < big)
total = total%5;
else
total = total - 5*big;
Then compare the remaining total to the number of small chocolates you have available.
Code:
// return the number of small chocolates necessary to make up the total
if (small >= total)
return total;
else
return -1;
By doing it this way I've also reduced your 4 if conditions to 2.
I forgot at the time I could manipulate the arithmetics like that. I was rather thinking in the mindset of "returning value X if satisfying condition Y", not "transforming X to Y under condition Z". Thanks.
Now I'm totally stuck here. ;-;
EDIT: I got an idea, lemme try it out.
Spoiler
5/
Spoiler
[COLOR="red"]Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
public int count7(int n) {
int count = 0;
int remainder = n%10;
if(remainder == 7) { // if find 7
count++; // increment count
n = count7(n/10); // continue checking the next digit -- This is where I'm wrong, it just doesn't work
}
else {
if(n>10) { // if still divisible by 10
n = count7(n/10); // proceed to check the next digit, through the procedure applied above.
} // Keep repeating until the condition is exhausted
else count = 0; // exhausted, found zero 7
}
return count;
}
I think I'm not applying a brake here for the recursion to end when it's done checking the left-most digit. I dunno how though.
Okay, I think I know. From working out a similar problem.
6/
Spoiler
[COLOR="red"]Given a positive int n, return true if it contains a 1 digit. Note: use % to get the rightmost digit, and / to discard the rightmost digit.
public boolean hasOne(int n) {
boolean result = false;
if(n%10 == 1) {
result = true;
}
else {
if(n>=1) {
result = hasOne(n/10);
}
else result = false;
}
return result;
}
Nevermind, I have no idea at all. In problem 6, once it returns true, it doesn't have to continue checking like when it finds a 7 in problem 5.
I think my brain exploded. Calling count7(n/10) is finding the count of 7's in the input n/10, not putting n/10 through the process of checking for 7's... If I call for a recursion on a method at halfway through it, does it evaluate up to that point, or the whole method?