algorithm for java

Thread Solved

Join Date: Mar 2008
Posts: 17
Reputation: soultrav is an unknown quantity at this point 
Solved Threads: 0
soultrav soultrav is offline Offline
Newbie Poster

algorithm for java

 
0
  #1
Nov 3rd, 2008
I am building an xml parser in java which will handle a catalog with some books (that have price and usefulness) and I want to implement an optimum algorithm that selects the books from the catalog which have the maximum usefulness while staying within the budget.
Example:
  1. <catalog cash=”100”>
  2. <book usefulness=”90” price=”60” >The Art of Computer Programming</book>
  3. <book usefulness=”90” price=”40”>Programming Pearls</book>
  4. <book usefulness=”60” price=”40”>American Psycho</book>
  5. </catalog>
,in this case the chosen books would be the first two books because they have a maximum usefulness of 180 and they stay in the budget.

Help would be appreciated,
Cheers
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: algorithm for java

 
0
  #2
Nov 3rd, 2008
here is some pseudo-code for you
  1. List<Book> usefulBooks;
  2. int cashAvailable = cash;
  3. booklist = query books by usefulness descending
  4. for(Book book : booklist)
  5. {
  6. if(cashAvailable - book.getPrice() > 0)
  7. {
  8. //add the book
  9. usefulBooks.add(book);
  10. //decrement our cash
  11. cashAvailable = cashAvailable - book.getPrice();
  12. }
  13. }
Last edited by ~s.o.s~; Nov 4th, 2008 at 10:46 am. Reason: Added code tags.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC