• Post Reply Bookmark Topic Watch Topic
  • New Topic

Going wrong in a logic problem  RSS feed

 
Dushyant Maheshwary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

Please help me out.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that "big" is used as part of the loop condition - but its value keeps changing.
 
Dushyant Maheshwary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What logic should I use there? I can't think of anything.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like chocolate.
 
Vaibhav Mittal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrote down the code just for fun. I love chocolate too.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed--tasty.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj kumar Sethi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Petar Ivanov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?"

Thank you in advance for your responses
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Thanks
 
Petar Ivanov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a problem from CodingBat ;)
http://codingbat.com/prob/p191363
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!