Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have 10 items with cost price and weight.my threshold cost is 1000 and threshold weight is 100kg.i want to take maximum number of items with in these threshold.
i.e. sum of the itemcost<threshold cost="" and="" sum="" of="" the="" weight="" less="" than="" threshold="">
Posted
Comments
[no name] 25-Oct-11 11:42am    
And where are you doing this? SQL? C#? JavaScript? Excel? In your head? You need to give more information.
jincyn 25-Oct-11 12:05pm    
iam doing in vb.net using asp.net

1 solution

This sounds like homework, but since you post this as a general question I'll give a general answer to your specific numbers (that is, I'm not sure I would recommend this for an arbitrary number of items).

Each of the ten items can either be included or not so you're looking at 2^10 possible combinations. Start with the collection of all ten items and see if that satisfies the cost and weight limitations. If not, look at the ten sets of nine items and see whether any of those satisfy the cost and weight limitations. Continue to eight, seven, six... items until you satisfy the conditions.
 
Share this answer
 
Comments
jincyn 25-Oct-11 12:34pm    
i want this in vb.net, can u plese give me the algorithm
JRickey 25-Oct-11 15:14pm    
With your question having the appearance of being homework I'm not going to provide code; the above is an informal algorithm that you can formalize. One other hint: after my earlier answer I thought about generalizing the problem to n items. This is a good candidate for recursion. For n=1 return 1 if that item meets the limitations, 0 if it does not. Otherwise, test whether the full set (all n items) meets the conditions, if so, return n; if not, perform recursion on the n sets of (n-1) items.
fjdiewornncalwe 25-Oct-11 16:41pm    
+5. Good answer to a homework question. I'd give a second +5 if I could for the comment.
jincyn 26-Oct-11 1:33am    
i am new in vb.net . plese provide the code
JRickey 26-Oct-11 12:04pm    
Even if you could convince me this is not a homework project (very unlikely), you will learn much from developing the code yourself. You may make mistakes in that code, in which case you post a question with the appropriate piece of code, describe the error you see and ask for help (and tag the question as homework). Besides, on the .Net platform my experience is in C#, not VB. I've broken the problem down into two parts that you can easily learn about on the web (collections, including how to remove one item from a collection; and recursive function done on the collection).

Make a strong effort on your side and I'll provide an additional hint for making the recursion more efficient.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900