combinations of variables , ambiguous thing

Hello,
lets say i have variables of type int:
a = 50
b = 150
c = 100
d = 200

And than i want create another variable, or some other type, lets call it water e.g.

Than i want to find all combinations for (a,b,c,d)
E.g. n will be 4 - (a,b,c,d) and r will be 2.

So for example, (a,b) (a,c) (a,d) in relation to this, variable "water" will change, it will have specific value it must have, lets say 200.

So program will find all combinations between (a,b,c,d), when n = 4 and r = 2.
And it will print out only one combination, in out case (a,b), because a+b = 200.
I hope i described it well, i can only read in english sry.




Last edited on
I would forget the combinations.

put the 4 values into a vector and sort that.

a very simple algorithm would save you a lot of work from here:
for each element in the array, solve X + known = value, eg

50+x = 200?
x = 150. search (binary, or hash-map, whatever) for 150. Is it there? Combination solved.
150 (already solved, if you remembered this, and marked it as used)
100+x = 200?
100. can a number combine with itself? If yes, solved, if not, does not exist.
and so on.

if you need to do sums of 3 values, then you have to iterate more. say 300..
50+ x + y = 300. put the next available value in and you get:
50+ 150 + x = 300? x = 100. that is in there ... combo found... still saves a lot of useless combos...

and you also know at some point that the higher stuff adds up too high. eg you know that 100+150 is too big, so all combinations of values bigger than 100 (with other values bigger than 100) are not worth looking at.

Last edited on
Topic archived. No new replies allowed.