The problem here in bat java coding made me stretch a bit:
Problem:
We want to make a package of whole kilograms of chocolate. We have small bars (1 kilogram each) and large bars (5 kilograms each). Return the number of small bars to use, assuming we always use large bars in front of small bars. Return -1 if this is not possible.
makeChocolate (4, 1, 9) β 4 makeChocolate (4, 1, 10) β -1 makeChocolate (4, 1, 7) β 2
http://codingbat.com/prob/p191363
I could come up with this as a solution, I'm not sure if this is the best way that any enthusiast would like to consider, with great pleasure! I typed this without any compiler or anything, so excuse me for the indentation:
ANY BEST ANSWERS WILL BE RECOGNIZED:
public int makeChocolate(int small, int big, int goal) { if(goal<5&&small>=goal) { return goal; } if(goal<5&&small<goal) { return -1; } if(goal>=5) { if(5*big>goal) { int temp=goal/5; if(goal-temp*5<=small) { return goal-temp*5; } if(goal-temp*5>small) { return -1; } } if(5*big<goal) { if(small<(goal-5*big)) { return -1; } if(small>=(goal-5*big)) { return goal-5*big; } } } return 0; }
source share