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

1. Member
Join Date
Sep 2011
Posts
5
Rep Power
0

## 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.
Java 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					                      }
}
}
}
}
}```
Last edited by sunde887; 09-08-2011 at 05:29 PM.

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

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

kind regards,

Jos

3. Member
Join Date
Sep 2011
Posts
5
Rep Power
0

## 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?

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

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

5. Member
Join Date
Sep 2011
Posts
5
Rep Power
0

## 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?

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

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

7. Member
Join Date
Sep 2011
Posts
5
Rep Power
0

## 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..;;

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

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

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

9. Member
Join Date
Sep 2011
Posts
5
Rep Power
0

## 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..

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

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

11. Member
Join Date
Sep 2011
Posts
22
Rep Power
0

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

wa.. how do i do this??????????????????????????????

12. ## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•