Re: Help with an algorithm to do the following:
- From: lndresnick@xxxxxxxxx
- Date: 11 Apr 2005 13:01:31 -0700
fadi wrote:
> Hello all,
>
> I am trying to implement the following, but I need help/suggestions
for an
> algorithm that will help achieve it:
>
> I have 1 item that has 3 categories (numerical values). I need to
find the
> right combination to get a matching desired total for all 3. For
example:
>
> Item1 | 60 | 40 | 10
> Item2 | 100 | 0 | 7
> Item3 | 10 | 50 | 13
> Item4 | 0 | 10 | 4
>
> If I am looking for: 420 | 100 | 54 then I would get the following
matching:
>
> 4 Item2
> 2 Item3
>
> What would be the best approach?
>
> Thanks!
> Fadi
How about something like this:
Create an array which as the maximum number of each item. For above,
that is
2,4,2,10 I think.
Do a nested iteration, summing as you go, from 0 up to the maximum
value for each member. For example, in above, do 0-10 copies of item
4. Then try 1 copy of item 3 and 0-10 copies of item 4, etc. Exit if
you get the exact match. If you fully iterate, no solution is
possible. I bet this could be optimized...
-David
.
- References:
- Help with an algorithm to do the following:
- From: fadi
- Help with an algorithm to do the following:
- Prev by Date: Re: Transferring Data between Screens
- Next by Date: Re: Length of variable names affect performance?
- Previous by thread: Re: Help with an algorithm to do the following:
- Next by thread: Re: Help with an algorithm to do the following:
- Index(es):
Relevant Pages
|