Results 1 to 12 of 12
  1. #1
    u4722839 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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
    buying 4 $20 item.

    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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by u4722839 View Post
    if there are items $87, $20, $99, $12 then the most I can spend is $100 since
    buying 4 $20 item.
    4x20 == 80. Your problem is a knapsack problem and google is your friend here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    u4722839 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by u4722839 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    u4722839 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by u4722839 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    u4722839 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by u4722839 View Post
    I think my brain is dead right now :).... cannot understand the algorithm probably hww..;;
    From your OP:

    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    u4722839 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by u4722839 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

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

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

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

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

Similar Threads

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 990
    Last Post: 07-24-2014, 03:28 AM
  2. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  3. Questions about the idea of the code
    By Innocent in forum New To Java
    Replies: 2
    Last Post: 04-03-2011, 09:42 PM
  4. Replies: 0
    Last Post: 03-21-2011, 09:46 AM
  5. No idea why my code won't work...
    By hopelessnoob in forum New To Java
    Replies: 11
    Last Post: 03-21-2011, 03:41 AM

Posting Permissions

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