So I have this problem where I can’t wrap my head around it. Here the assignment:
A programmer walks into a flower shop. He has $100 in his pocket, and wants to spend all his money to buy roses. However, there 3 types of roses: white ones, yellow ones and red ones.
Yellow ones cost $5 each, white ones cost $7 each and red ones cost $10 each.
How many combinations are there so that he can buy at least one rose of each color? And he has to spend all the money he has, he shouldn’t be able to buy any roses for the money left.
So I know it’s about for loops here but I don’t know/understand how to organize the loop, how do I set the data and get to this combination number? Can you please help me out with this?
I don’t know where to start since I really don’t understand how to do this…

Hi,
How are you? I have read your problem and I will try my best to help. I haven’t gotten to that part yet but, what I would do is solve the problem first. Then work your way up from there.

Generate each combination and check whether they meet the condition

This gets really obvious really quick if you just start poking at it. You can also refer to how you would generate combinations manually, study what you do. For example, how would you manually generate all combinations of a pair of 6-sided dice? How do you do it for one 6-sided die? What was the difference? How would you apply that difference again to go from two dice to three dice?

I’ve tried to come up with some kind of solution but it’s partial and I still don’t understand it very well. Can you please help me out with my code?

public class Program
{
public static void main(String[] args)
{

int money = 100;
int whitePrice= 5;
int yellowPrice = 7;
int redPrice = 10;
int combinations= 0;

for (int i = 0; i < money; i+=5)
{
for (int j = 0; j < money; j+=7)
{
for ( int k =0; k < money; k+=10)
{
if (money - whitePrice + 1 < (i / whitePrice) + (j / yellowPrice) + (k / redPrice) < money)
{
combinations++;
System.out.println(“You can buy " + i + " white roses, " + j + " yellow roses " + " and " + k + " red roses.”);
}
}
}
}

System.out.println(“There is " + combinations + " number of combinations you can use to buy your roses.”);
}

}

so, problem No. 1 - the code doesn’t work.
problem No. 2 - in the if statement I want to set the condition that the sum of money per rose divided by price per rose is less than 100 and more then 96 (where one can’t buy any more roses for $4)
problem No. 3 - where did my logic go wrong here?

I appreciate you trying to help, but being unable to solve the problem is actually the problem I have. Please have a look at my code and give me your feedback. Thanks!

Don’t have much to say about it. It’s somewhat close, but your condition is way off
Maybe start by writing down in plain english what you at all need to test for?

Which part is testing that you have one of each? That’s why I suggested that you should go back to the instructions and think about what you’re testing, and write it down in plain english. Otherwise you’re so busy trying to make sense out of code that you don’t know what you’re supposed to write in the first place.

You also don’t seem to have decided whether i j k are number of flowers or amounts of money.

yeah, so I made up my mind. i,j,k are numbers of flowers. And through my condition I want to check what are combinations of buying different numers of flowers, while the amount spent is greater than the money after deducting the price of the cheapest rose, but smaller than the “money” amount. Since this amount I am checking is greater than 95 and smaller than 100 it is obvious than none of i,j,k can actually be zero, right?

public class Program
{
public static void main(String[] args)
{
int money = 100;
int whitePrice= 5;
int yellowPrice = 7;
int redPrice = 10;
int combinations=0;
for (int i = 1; i < money/whitePrice; i++)
{
for (int j = 1; j < money/yellowPrice; j++)
{
for ( int k = 1; k < money/redPrice; k++)
{
if (money-whitePrice < (i * whitePrice) + (j * yellowPrice) + (k * redPrice) && (i * whitePrice) + (j * yellowPrice) + (k * redPrice) =< money)
{
combinations++;
System.out.println(“You can buy " + i + " white roses, " + j + " yellow roses " + " and " + k + " red roses.”);
}
}
}
}

System.out.println(“There is " + combinations + " number of combinations you can use to buy your roses.”);
}
}

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.

so if you don’t want to give me answers, can you ask me some useful questions then? For loops are not clear enough? The condition is unclear or complicated? I explained what I wanted to achieve through my condition. Where do I go from here…

There is an argument to be made for that 96, 97, 98, 99, 100 are all acceptable spent amounts, the author does somewhat suggest that this is their intention:

But if he spends ALL money, then there is 0 left, which he can’t buy any more roses for.

That’s easy to test, just change “is it 100?” to “is it above 95?”

int totalSpent = …;
boolean hasOneOfEach = …;
if (totalSpent == 100 && hasOneOfEach) {
…
}

but what if the amount cannot be exactly $100 and I will get no result? If all the conditions are met and he spends say $97, I guess I have to put some kind of leeway in there.

Generating only at-least-one-of-each combinations would ensure that you’re meeting that requirement, yeah. But:

You can easily construct a combination that adds up to 100 to show that this isn’t the case:

10 * 7 = 70
1 * 10 = 10
4 * 5 = 20

I’d be okay with an answer of 0 though, that is an answer too.
Again, the question is somewhat open to interpretation (I don’t think it is, it clearly says ALL money, and that means 100, but this may be a bug in the problem statement. I would do what it says, not what it might mean)

What’s wrong is the things mentioned. It doesn’t ensure that there are one of each. As it was written, it was very difficult to read and understand what it was testing. And strictly speaking, only a spent amount of 100 is a solution with how the problem statement is worded.

I made it work, finally.
I think I may have problem with steps leading to writing the code actually. I’m having a hard time simplifying the problem before identifying the steps to make, I-ll have to work on that. the code works now, thank you for your input @ionatan
And just one edit to the task: he should spend all his money to buy roses, and if any change is left than he should be unable to buy any more roses with that change. So basically it has been said that he should spend everything, or at least most of it.
Here’s my code

public class Program
{
public static void main(String[] args)
{

int money = 100;
int whitePrice = 5;
int yellowPrice = 7;
int redPrice = 10;
int combinations = 0;

int wNumber = money - (yellowPrice + redPrice);
int yNumber = money - (whitePrice + redPrice);
int rNumber = money - (whitePrice + yellowPrice);
int minAmount = money - whitePrice;

for (int i = 1; i < wNumber; i++)
{
for (int j = 1; j < yNumber ; j++)
{
for ( int k =1; k < rNumber ; k++)
{
if ((i * whitePrice) + (j * yellowPrice) + (k * redPrice) < money && (i * whitePrice) + (j * yellowPrice) + (k * redPrice) >minAmount)
{
combinations++;
System.out.println(“You can buy " + i + " white rose(s), " + j + " yellow rose(s) " + " and " + k + " red rose(s).”);
}
}
}
}

System.out.println(“There is " + combinations + " number of combinations you can use to buy your roses.”);
}