Looking for good algorithm to calculate a list of figures

Hello,
Given a list of figures:
1899.09
345.55
300.00
130.05
90023.00
45.90
120.50
8831781.56
505.00
45.55
Then the program need to find all the possible addition of figures that will able to produce a answer, for example, 550.55
Then the possible additions are:
300.00 + 120.50 + 130.05 = 550.55
505.00 + 45.55 = 550.55
any good algorithm or sample code?
Thanks for any help.

A general knapsack problem solver is overkill here if one just wants to find a combination that fits 'perfectly'. If the list of numbers is small (< 100), a simple exhaustive search will do fine (read: it's fast). Have a look at the following sample class that solves this particular problem. Note that the set of numbers is a set of integer numbers, because floating point numbers don't add up well; in particular large and small numbers added will result in very inaccurate sums.
kind regards,
public class Comb {
     private int   u; // index of used numbers sofar
     private int   s; // the sum to be found
     private int[] c; // the candidate numbers
        // given a set of numbers and a sum, instantiate a solver
     public Comb(int[] c, int s) {
          this.c= c;
          this.s= s;
          this.u= c.length;
        // yes, swap two elements in array c
     private void swap(int i, int j) {
          int t= c;
          c[i] = c[j];
          c[j] = t;
// use the number at index i as a try
     private void use(int i) {
          swap(i, --u);
// unuse the number at index i
     private void unuse(int i) {
          swap(i, u++);
// proccess (e.g. print) a current solution
// note that this method could throw an exception if
// just one solution is needed.
     private void solution() {
          for (int i= u; i < c.length; ++i) {
               System.out.print(c[i]);
               if (i < c.length-1)
                    System.out.print("+");
          System.out.println("="+s);
// find all solutions to this problem
     public void find(int t) {
          if (t == 0) // ready?
               solution();
          else if (t > 0) // still more to do?
               for (int i= 0; i < u; ++i) {
                    use(i);
                    find(t-c);
                    unuse(i);
// solve the example problem ... == 11
     public static void main(String[] args) {
          int c[]= { 5, 3, 6, 8 };
          int s = 11;
          new Comb(c, s).find(s);

Similar Messages

  • Looking for good JSP editors??

    Hi,
    I am new to JSP but very old to ASP. I am looking for good JSP editors if there are any for free to dl.
    Do we have any JSP editors...like in Microsoft which gives you dropdown list of parameters and properties.methods... ???
    Thanks,
    Kavans.

    Sounds like your looking for a good IDE. As far as free ones
    go, netbeans is as good as any. Comes with a lot of nice
    features.. such as code completion, and has the latest version
    of tomcat integrated.

  • Looking for good orchestra plugins

    Hi,
    I am looking for good classical orchestra plugins (violins cellos etc...)
    What are your suggestions ?
    thanks

    As soon as iS sees this, he'll chime in with his
    faves...;)
    My ears were burning.
    I use bits and pieces from many different libraries. For me, there's no one library that has all the right textures, articulations, etc. for any one instrument.
    I recently purchased the Kirk Hunter EXS24 library. Most of the brass, strings, and woodwinds are superb. The caveat here is that almost all samples were recorded in some kind of hall or soundstage, so as with other libraries that were sampled with ambience, it can sometimes be problematic if you're going after a dryer sound. Depending on the sound, careful tweaking of the release time can minimize that effect.
    So yes, I endorse Kirk Hunter. Bang-for-buck quotient is great ($325).
    GPO -- I use their woodwinds and harp, the occasional solo string sound, percussion (the xylophone and celeste are wonderful). But the strings are pathetic and the brass poor. Still, for $250 it's an astounding deal, and worth it for the woodwinds/harp alone. It's paid for itself 50x over.
    A favorite for strings are Sonic Implants. My copy went through a Gigasampler -->EXS conversion and I love them to death.
    EWQLSO are not a fave. I've heard good things about Gold though.
    Vienna... I've never been impressed with any of their demos. Well crafted but flat and lifeless sounding. I think the fault is with the demos and not the actual samples. Anyway, a friend recently realized a part for me using some of their specialty brass sounds and it was exactly what the doctor ordered! One day I'll invest in VSL and add it to the arsenal.

  • I am looking for good pdf conversion program like omni pro 18

    i am looking for good pdf conversion software, like omnipage pro 18 by nuance, it is only supported by windows, what is good?

    What are you trying to convert the pdf to (MS word, text, what)?
    Here's one for word: PDF Converter
    Possibly another way for word: How to Convert PDF to Word Document on a Mac

  • Looking for Good Article/Explanation on Timezones

    Hi,
    I'm looking for answers about timezones and if anyone could point me to a really good artilce I'd appreciate it. Here's the type of thing I'm looking for...
    I'm writing an application that creates records in a file. The records are timestamped as to their creation. The catch is that the timestamp may not be the default timezone depending on the user's preference. I know the user's UTC offset and whether they use daylight saving time. The problem comes in when trying to figure out which timezone id to use to create the new timezone and set it in the calendar object. For example, if I know that the GMT offset is -7*60*60*1000, I get back 7 possible ids. I don't know anything about the rules for daylight saving time for the user, only if they use it or not. How do I know which id to choose? The users can be from anywhere in the world.
    Thanks for any help.

    Yes, I'd thought about just having the user set their
    timezone (similar to the forum Settings). However,
    even this forum settings causes some confusion.
    There's two options for a number of timezones. What
    differentiates them? The daylight saving rules are
    important to know and the list of timezones doesn't
    indicate this in any way. This list doesn't show all
    available timezones either (as previously mentionted,
    there's seven timezones for GMT -0700).Then you would have to do a better job than Sun did, wouldn't you? After all if you are going to be interpreting the answer you get, you had better ask a clear question. If you want to know the list of timezones actually supported by Java, then examine the source code for the TimeZone class. You can also search Google for "time zones" for links that will keep you busy for weeks learning the arcana of time zones around the world.

  • Looking for good Isync Phone (other then Iphone)

    my current phone just died. I'm currently using Verizon I'm looking for a good phone that will be able to use all the Isync stuff Address book w/ Pics calender etc.. Does anyone know of a good phone phone that has a keyboard and will be capable via bluetooth to use isync

    Compare the list below:
    http://www.apple.com/macosx/features/isync/index.html
    with what's available from Verizon...

  • Looking for good easy to use inexpensive tuner

    Hi,
    I am looking for a good, easy to use, inexpensive tuner. Not one you use in a rack or on a pedal, but one you use on the computer. I looked and there are a ton out there so I figured I would look for recommendations here.
    I do not like the way GBs works. It fluctuates too much as the note decays and drives me nuts. I want a simple one, that works great. (If it has a feature that helps in tuning a 12 string, than thats a plus, but not a deal breaker). Simple and accurate is most important.
    Lastly I noticed some they sell are AU and some are stand alone. Is one better than the other?
    Thanks,
    Paul

    Strangedogs,
    Thanks for the link but if you read the review of it on the site, it is terrible. The users on VT hated it. It got 1 star out of 5. I was looking for a recommendation of one that someone has actually "used" themselves and likes it from experience using it.
    There was a link that someone posted there for a free tuner from emagic that they said is great, but all the links I found with google run to a dead end unfortunately.
    Paul
    BTW: boy do those 12 strings take forever to tune. I was jamming with a friend over new years weekend and it took him forever to tune up, (and he has an excellent ear).

  • Looking for a VI to calculate phase difference

    I am looking a VI that will calculate the Phase difference as a function of time between two sine waves at 20 kHz. I am using LabView7 Express just so we're on the same page.

    Hi,
    There is a Extract Single Tone Information.vi that is available from All Functions >> Analyze >> Waveform Measurements. This VI takes a time signal in and returns a detected phase (deg) out. Therefore, you can use one of these VIs for each sine wave and take that difference to determine the phase difference of your sine waves as a function of time.
    Note that the Extract Single Tone Information.vi is not available LabVIEW Base package, but is available in all other LabVIEW packages.
    Thanks again and have a great day!
    Kileen C.
    Applications Engineer
    National Instruments

  • Looking for good headphones...

    OK, I'm looking for a new pair of good headphones. For a few years, I've used Sony's. I like them, but I have one problem: durability. All of them have gone dead in a year or two. To be honest, I may be considered to be abusing them - They've all been earbuds, and I have a tendency to just dump them in my pocket. I bought a pair of $50 ones at the store about six months ago, and they work fine, but I've just noticed that the wire is exposed in one area, which generally means that they're gonna die soon. A case came with them, that was good, but it broke after a month or so.
    I guess what I'm looking for is this:
    - A pair of earbuds
    - Good sound quality (doesn't have to be noise reduction, in fact, I might prefer they weren't, because I generally need to be able to hear what's going on around me)
    - durability
    - different size 'buds' (My ears are pretty small; the normal ones that came with the iPod were too big, they won't stay in, and all other one-size-fits-all type buds pose the same problem)
    - $100 or less
    - a durable, easy to use case (that I won't just ditch because there's too much work getting them in and out)
    I was looking around and found that I really like the Panasonic RP-HJE70-S, they're around $100 and come with a case. They seem to be good, and the few reviews I've found like them. But they're fairly new, and there isn't much word out there yet. I also wanted to get some opinions first.
    Thanks.

    Hi,
    I splurged recently on a pair of Audio Technica ATH M50's which were very expensive.
    They sound wonderful and are 8 out of 10 comfortable when using them for hours. They're really good for tracking but I have found them to be highly inaccurate/coloured for mixing.
    I don't know if this is important to you, but I've been unpleasantly surprised that most headphones seem to have the cable hard wired to the enclosure.
    My fantastic Beyer Dynamic DT 150's (which are now 20 years old but not so good for tracking or mixing) have a hard plastic tab at the end of the cable that plugs into the headphones, and this tab has several, 'male' D.I.N. like pins (except in a rectangular formation) which plug into their female counterparts in the enclosure. This means that if the cable ever breaks (which, ironically, it hasn't because it is very durable) you can just buy another one and plug it in.
    I wonder if there are other elements of design that are important to you or others.
    Let us know what you choose, and you might want to check out the other sites too, homerecording.com, macrumors.com, audiorecording - I'm sure you're familiar with a lot of these.
    Good luck
    Message was edited by: drutgat

  • Looking for good first example for RSS

    Hello,
    I am looking for a good code example which will guide me how to use RSS at the portal. Can anyone please direct me to such example?

    Hi Roy,
        I am posting a xslt that filters the news by searching for any news that has "Troops" and "Pope" in the title of the news. I can send you the complete par if you email me your address at [email protected] You owe me lunch buddy. :). The following xslt is for http://rss.news.yahoo.com/rss/world feed.
    [code]<?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/rss/channel">
    <a class="urLnk"><xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute><xsl:value-of select="title"/></a>
    <xsl:for-each select="item">
    <xsl:variable name="titlestring"><xsl:value-of select="title"></xsl:value-of></xsl:variable>
    <xsl:variable name="pope">Pope</xsl:variable>
    <xsl:variable name="troops">Troops</xsl:variable>
    <xsl:if test="contains($titlestring,$pope) or contains($titlestring,$troops)">
    <table>
        <tr>
             <td class="urTxtStd">
                  <a class="urLnk"><xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute><xsl:value-of select="title"/></a>
             </td>
        </tr>
        <tr>
             <td class="urTxtStd">
                 <xsl:value-of select="description"/>
             </td>
        </tr>
    </table>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>[/code]
    Message was edited by: Prakash  Singh

  • Looking for good docs for securing VoIP enviornment

    I'm looking for documents on securing IP phones. Mostly resricting the information available from the phone itself via the settings button.
    Also a document listing the required protocols for a functional voice VLAN would be helpfull.
    Thanks,
    Andy

    Hi Andy
    for Cisco IP Phones with CCM did you try Cisco Unified CallManager Security Guide, here is the link:
    CCM 5.x
    http://www.cisco.com/univercd/cc/td/doc/product/voice/c_callmg/sec_vir/ae/sec502/index.htm
    CCM 4.1
    http://www.cisco.com/univercd/cc/td/doc/product/voice/c_callmg/4_1/sec_vir/ae/sec413/index.htm
    hth
    /majdi

  • Used to use InterAKT menus - Looking for good menu creator

    Looking for a way to create attractive menus in Dreamweaver
    CS3. My InterAKT menu creator is not compatible with CS3, and I
    need to find a way to create attractive looking menus easily. Any
    suggestions? I am on an apple computer.
    The Spry menu bar in CS3 looks very basic, and the
    documentation seems lacking. Is there a good way to create great
    menus in CS3, or did I have to get a plug-in? And is so, which one?
    Thanks!

    The following have some great menu creation menus. I
    generally use p7 menu's
    myself.
    http://www.projectseven.com/index.htm
    http://www.pluginlab.com/dreamweaver-extensions
    k

  • Looking for good quality head set

    I am looking for a good quality in head set for my 8100 pearl. The problem is that all the really high quality headphones have a 3.5mm plug. Does any use the 2.5mm in ear sound isolating head set sold on the Blackberry site? Do they have a good quality sound? 

    Oh, so for a Browser/Internet, not the OS!
    A Slider...
    http://download.oracle.com/javase/tutorial/uiswing/components/slider.html
    Here's one Menu builder...
    http://dhtml-menu.com/
    Not sure what the requirements are, but it looks like it uses Java.

  • Looking for good book on using DSLR for shooting video

    I recently purchased a new Canon t3i DSLR. I've been watching videos on YouTube and it seems like this camera is capable of taking great HD video. My test videos look nothing like these examples. I am looking for a good resource that will teach me what settings to use, especially manual settings for shooting videos. Of course it would be great to find something specific to my camera, but I'm sure that the techniques will apply to most DSLRs.
    Thanks

    This website is very useful
    http://nofilmschool.com/

  • Looking for good 10g reference book

    I am looking for a recommendation for an all purpose 10g reference book

    Hi,
    a good complete reference of 10g Database can be found at:
    http://www.amazon.com/exec/obidos/ASIN/0072253517/oracleinternetse
    Regards,
    Simon

Maybe you are looking for

  • What is a Voicemail password and what does it do?

    What is a voicemail password and what does it do?

  • Procedure for read/write an XML-File using UCCX9

    Hi Team! Is there someone who can explain me the procedure of reading/writing an XML-File using Cisco UCCX Rel. 9.0? In the last couple of days I did a lot of research on the partner community as well as reading the CRS-Editor-PDFs. But I can't find

  • Regarding watermark in 10g

    Hi Experts, is it possible to add watermark on dashboard reports in 10 g. please suggest any solution.

  • Flash File too Large

    I have a flash file that is 28K KB. I cannot get it to open, and definately cannot publish the file. At this size, is there any way to break the file up, or even to get it open so that I can do that? Thx,

  • BB playbook Bridge message app won't open.

    I updated my OS today and after that the BB Playbook Bridge message app, when trying to open, just blinks like constant rápid reloading and does not open even after restarting the playbook. Other bb Bridge apps work fine. How to fix this?