This sounds like a more complex version of the classic "animal legs" problem. Except in that case, you only have two animals to deal with, so you could work it out using elementary maths ("every time I swap a cow for a chicken, I get two more legs...").
But in this case you have more variables, so you'll need to resort to the algorithmic way. Recall that in the animal legs problem, you get something like this:
number_of_chickens2 + number_of_cows4 = number_of_legs
number_of_chickens + number_of_cows = number_of_heads
number_of_legs and number_of_heads are known, so substitute the equations into each other.
In your example, there are many more variables - more "animals" - which looks something like this:
number_of_foo + number_of_nachos + number_of_pizzas2 + number_of_bars3 + number_of_cakes*4 = 24
number_of_foo + number_of_nachos + number_of_pizzas + number_of_bars + number_of_cakes = 8
A typical way to solve this is to express this as a matrix and perform row reduction on it, the simplest method of which (both by hand and by computer algorithm) is Gaussian elimination. You may have been taught this in high school.
| 1 1 2 3 4 | 24 |
| 1 1 1 1 1 | 8 |
(swap row1 and row2)
| 1 1 1 1 1 | 8 |
| 1 1 2 3 4 | 24 |
(row2 = row2 - row1)
| 1 1 1 1 1 | 8 |
| 0 0 1 2 3 | 16 |
(row1 = row1 - row2)
| 1 1 0 -1 -2 | -8 |
| 0 0 1 2 3 | 16 |
Now that the equations are in reduced row echelon form, you can start plugging in values to see which ones work. Note that your matrix isn't completely reduced, which tells you there are many possible solutions.
Let's start with row 2: it is basically saying: number_of_pizzas + number_of_bars*2 + number_of_cakes*3 = 16
. Obviously keep in mind that you can't have negative numbers of anything, which implies that you can't have more than 8 of anything. With that in mind, we can plug in 8 for the number of bars, which means 0 pizzas and 0 cakes:
01 + 82 + 0*3 = 16
Now put those values into row 1:
number_of_foo1 + number_of_nachos1 + 00 + 8-1 + 0*-2 = -8
number_of_foo + number_of_nachos = 0
Now we have our first solution: 8 bars.
Going back to the matrix, since any set of values that satisfies those row equations work, we can also try 5 cakes, which forces us to choose 1 pizza:
11 + 02 + 5*3 = 16
Into row 1:
number_of_foo1 + number_of_nachos1 + 10 + 0-1 + 5*-2 = -8
number_of_foo + number_of_nachos = 2
Now we have a second set of solutions, as long as the number of foo and nachos total 2 (they cost the same so it doesn't matter), and there's 1 pizza and 5 cakes.
Let's try one more, just to get the hang of it. We can have 4 cakes, and 2 bars, which leaves 0 pizzas:
01 + 22 + 4*3 = 16
Into row 1:
number_of_foo1 + number_of_nachos1 + 00 + 2-1 + 4*-2 = -8
number_of_foo + number_of_nachos = 2
Again, we have 2 foo-or-nachos, but this time also 2 bars and 4 cakes.
You may be wondering why we're guessing the values at the end - isn't there an algorithmic way to solve this? Sure, there is, it's called integer programming which is unfortunately NP-hard. So guessing, or going through all the possibilities, should be good enough.