# Got the idea but cannot write in code... help me

• 09-08-2011, 03:46 PM
u4722839
Got the idea but cannot write in code... help me
Question: Suppose you are given a gift voucher worth \$100. Assuming that the shop does not give change on gift voucher and you do not wish to spend any more than what is in the gift voucher. This means for example:

if there are items \$87, \$20, \$99, \$12 then the most I can spend is \$100 since

I have wrote some of the codes but...stuck.
Code:

```public class lab233 {         static int voucherMoney = 100;         static int item1 = 23;         static int item2 = 38;         static int item3 = 40;         static int item4 = 58;                 static int a = voucherMoney/item1; //by dividing most no of this items can be bought         static int b = voucherMoney/item2;         static int c = voucherMoney/item3;         static int d = voucherMoney/item4;                 public static void main(String[] args){                 for(int i = 0; i < a; i++){                         for(int j = 0; j < b; j++){                                 for(int k = 0; k < c; k++){                                         for(int g = 0; g < d; g++){                                 //I think by using 4 loops and compare the combinations.... by using compareTo methods??? help me here T.T                                                              }                                 }                         }                 }         } }```
• 09-08-2011, 03:51 PM
JosAH
Re: Got the idea but cannot write in code... help me
Quote:

Originally Posted by u4722839
if there are items \$87, \$20, \$99, \$12 then the most I can spend is \$100 since

kind regards,

Jos
• 09-08-2011, 03:56 PM
u4722839
Re: Got the idea but cannot write in code... help me
5 \$20 my bad lol thanks!..hmm.. how should i type..to find out the model solution for this then?
• 09-08-2011, 04:38 PM
JosAH
Re: Got the idea but cannot write in code... help me
Quote:

Originally Posted by u4722839
5 \$20 my bad lol thanks!..hmm.. how should i type..to find out the model solution for this then?

As I wrote: google for "knapsack problem"; I also wrote a complete code solution for this nasty little problem in this forum once; maybe the seach facility can find it for you.

kind regards,

Jos
• 09-08-2011, 07:43 PM
u4722839
Re: Got the idea but cannot write in code... help me
I search for the google and saw your example but I cannot apply to this situation... can you help me directly here???? anyone?
• 09-08-2011, 07:51 PM
JosAH
Re: Got the idea but cannot write in code... help me
Quote:

Originally Posted by u4722839
I search for the google and saw your example but I cannot apply to this situation... can you help me directly here???? anyone?

Why do you think my solution can't be applied to your 'situation'?

kind regards,

Jos
• 09-08-2011, 08:02 PM
u4722839
Re: Got the idea but cannot write in code... help me
I think my brain is dead right now :).... cannot understand the algorithm probably hww..;;
• 09-08-2011, 08:09 PM
JosAH
Re: Got the idea but cannot write in code... help me
Quote:

Originally Posted by u4722839
I think my brain is dead right now :).... cannot understand the algorithm probably hww..;;

Quote:

if there are items \$87, \$20, \$99, \$12 then the most I can spend is \$100
So you have to fill a knapsack of 'size' 100 given the items: 87, 20, 20, 20, 20, 20, 99, 12, 12, 12, 12, 12, 12, 12, 12

kind regards,

Jos
• 09-08-2011, 08:22 PM
u4722839
Re: Got the idea but cannot write in code... help me
hmm................I think I should give up... cannot write the code... T.T among those values how can I get the maximum combination that is equalto or less than \$100... the combination............hmm..
• 09-08-2011, 08:29 PM
JosAH
Re: Got the idea but cannot write in code... help me
Quote:

Originally Posted by u4722839
hmm................I think I should give up... cannot write the code... T.T among those values how can I get the maximum combination that is equalto or less than \$100... the combination............hmm..

Yes, you'd better give up; I spelled it almost out for you, you found my code on this forum and I gave you the way to use it for your 'situation'; how much more spoonfeeding do you need?

kind regards,

Jos
• 09-09-2011, 03:28 AM
gomdohri
Re: Got the idea but cannot write in code... help me
wa.. how do i do this??????????????????????????????
• 09-09-2011, 03:33 AM
Junky
Re: Got the idea but cannot write in code... help me
Try adding a few more question marks. I'm sure that will convince someone to write the code for you.