954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

algorithm for java

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:

<catalog cash=”100”>
<book usefulness=”90” price=”60” >The Art of Computer Programming</book>
<book usefulness=”90” price=”40”>Programming Pearls</book>
<book usefulness=”60” price=”40”>American Psycho</book>
</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

soultrav
Newbie Poster
17 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

here is some pseudo-code for you

List<Book> usefulBooks;
int cashAvailable = cash;
booklist = query books by usefulness descending
for(Book book : booklist)
{
if(cashAvailable - book.getPrice() > 0)
{
//add the book
usefulBooks.add(book);
//decrement our cash
cashAvailable = cashAvailable - book.getPrice();
}
}
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You