Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Going wrong in a logic problem

Greenhorn
Posts: 7
Hi everyone, I'm trying to write the code for the following logic problem:

We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.

Here is my code:

For most of the cases, I'm getting the correct output, but for the following two inputs, I'm not getting the desired output:
makeChocolate(9, 3, 18) (here, the output should be 3, but the actual output is coming to 8)
makeChocolate(1, 2, 7) (here, the output should be -1, my output is coming 2)

Rancher
Posts: 42975
76
The problem is that "big" is used as part of the loop condition - but its value keeps changing.

Dushyant Maheshwary
Greenhorn
Posts: 7
Thank, didn't notice that! That helped me solve my first problem. But I'm still getting the wrong output for the 2nd input. Any help there?

Sheriff
Posts: 21288
87
In your second example, small + big * 5 is 11 and so larger than goal. It therefore goes into the loop.

Dushyant Maheshwary
Greenhorn
Posts: 7
What logic should I use there? I can't think of anything.

Ranch Hand
Posts: 3090
14
You might try a different approach entirely. Let's say small is 1, while big is 1000000. And goal is 123000456. How many small bars do you need? Don't write a program yet - just think about what the answer should be. What is your answer? And more importantly (at least, assuming your answer is correct) how did you arrive at that answer?

Rob Spoor
Sheriff
Posts: 21288
87
Since big bars always have a precedence over small bars, first check how many big bars fit in the goal; you can use simple int division for that. You then get a new goal: the number of small bars required to fill the remainder. Keep in mind that the number of big bars that fit in the goal can be smaller or larger than the number of available big bars. You'll need the minimum of the two; you can't use more big bars than you have, and you can't add more big bars than your goal allows.

Author
Rancher
Posts: 12617
I like chocolate.

Greenhorn
Posts: 23
Wrote down the code just for fun. I love chocolate too.

David Newton
Author
Rancher
Posts: 12617
@Vaibhav: We tend to discourage posting complete solutions; rather we prefer to give hints or suggestions and let the person trying to solve the problem figure it out for themselves. Please see DoYourOwnHomework for some reasons why. Thanks!

Vaibhav Mittal
Greenhorn
Posts: 23
@David I am sorry. I had a lot of free time and nothing to do. So decided to give it a try. Will be careful next time.

I still love chocolate.

David Newton
Author
Rancher
Posts: 12617
Agreed--tasty.

Sheriff
Posts: 3015
12
It seems to me the right answer for makeChocolate(1, 2, 7) is 2. 1 big bar + 2 small bars = 7 kg. In other words, you're done. Why do you think it should be -1?

Mike Simmons
Ranch Hand
Posts: 3090
14
Hm, it looks like earlier, I read the problem too quickly and misinterpreted how the "big" and "small" variables were being used. So ignore my last post. However, I would still suggest that there's another way to do this sort of problem which is much simpler & quicker, and involves no loops.

Rob Spoor
Sheriff
Posts: 21288
87

Greg Charles wrote:It seems to me the right answer for makeChocolate(1, 2, 7) is 2. 1 big bar + 2 small bars = 7 kg. In other words, you're done. Why do you think it should be -1?

Because you don't have 2 small bars, you only have 1. The only goals you could meet are 0, 1, 5, 6, 10 and 11.

Rob Spoor
Sheriff
Posts: 21288
87

Mike Simmons wrote:However, I would still suggest that there's another way to do this sort of problem which is much simpler & quicker, and involves no loops.

Like my suggestion to use division and a few if-statements?

Mike Simmons
Ranch Hand
Posts: 3090
14
Yeah, originally I was trying to avoid being so obvious about it, trying to prompt the original poster to figure that out on their own. But that message got obscured by my misunderstanding of the role of "big" and "small" - I knew a loop was unnecessary and inefficient, so didn't look too closely at it. Then you went for the blunt approach I had been trying to avoid. Absent any subsequent feedback from the original poster, I don't suppose it matters much either way.

Greenhorn
Posts: 1

Ulf Dittmer wrote:The problem is that "big" is used as part of the loop condition - but its value keeps changing.

Use this code:

while(goal>=5 && big>0)
{
goal-=5;
--big;
}
if(goal<=small)
return goal;
else
return -1;

its working for all sets.

lowercase baba
Bartender
Posts: 12613
50
Raj,

Normally, I would delete your solution. We don't hand out answers here. Instead, we encourage people to work it out on their own. We suggest, hint, give tips, make minor corrections to code the original person posts...but simply handing out the complete answer is against our policy.

The only reason I'm not going to bother deleting this is that you have replied to a thread that has been inactive for over two years. I doubt they are still looking for an answer.

Marshal
Posts: 58345
178
Welcome to the Ranch
I would have formatted your code with code tags, but your code isn’t indented. you should use correct indentation and code tags in future.

Greenhorn
Posts: 3
Hi to all I am new in Java as you will see later in this post and I tried to find the solution of this task. Firstly I tried to do it without loops and I could created just this long code :

Which do all the sets without the so called "Other test". Then I decided to do it with loops and I wrote this long code:

which do the job, but as I see there are far simpler solutions.

However, my question is "Is it possible to do it without loops and where did I go wrong in my first try?"

Campbell Ritchie
Marshal
Posts: 58345
178
Welcome to the Ranch

Ranch Hand
Posts: 225
5
Looking at this, wouldn't the return value of small bars always be the target goal? If that is the case, and I am not misunderstanding the specs, it becomes a simple matter of (large * 5) + small >= targetgoal.

Petar Ivanov
Greenhorn
Posts: 3

Campbell Ritchie wrote:Welcome to the Ranch

Thanks

Petar Ivanov
Greenhorn
Posts: 3

Robert D. Smith wrote:Looking at this, wouldn't the return value of small bars always be the target goal? If that is the case, and I am not misunderstanding the specs, it becomes a simple matter of (large * 5) + small >= targetgoal.

Not always, because you can have a situation like this one - goal = 18, big = 4 and small = 2. 4*5+2>18, but it wouldn't be correct because you haven't the needed 3 small. You have only two.

Bartender
Posts: 2155
46
This is a problem from CodingBat ;)
http://codingbat.com/prob/p191363