Click here to Skip to main content
15,892,537 members
Home / Discussions / C#
   

C#

 
GeneralRe: WebBrowser - Fetching HTML code from developer tool Pin
AshwiniSH28-Oct-13 19:42
professionalAshwiniSH28-Oct-13 19:42 
GeneralRe: WebBrowser - Fetching HTML code from developer tool Pin
Richard MacCutchan28-Oct-13 22:44
mveRichard MacCutchan28-Oct-13 22:44 
GeneralRe: WebBrowser - Fetching HTML code from developer tool Pin
AshwiniSH28-Oct-13 23:11
professionalAshwiniSH28-Oct-13 23:11 
GeneralRe: WebBrowser - Fetching HTML code from developer tool Pin
Richard MacCutchan28-Oct-13 23:41
mveRichard MacCutchan28-Oct-13 23:41 
GeneralRe: WebBrowser - Fetching HTML code from developer tool Pin
Dave Kreskowiak29-Oct-13 2:45
mveDave Kreskowiak29-Oct-13 2:45 
GeneralRe: WebBrowser - Fetching HTML code from developer tool Pin
AshwiniSH29-Oct-13 2:58
professionalAshwiniSH29-Oct-13 2:58 
GeneralRe: WebBrowser - Fetching HTML code from developer tool Pin
Dave Kreskowiak29-Oct-13 3:08
mveDave Kreskowiak29-Oct-13 3:08 
QuestionCombination Algorithm Pin
santosh_011428-Oct-13 0:38
santosh_011428-Oct-13 0:38 
XML
I am trying to write a small code to find the possible combinations from a list of integer values which when added is equal to the input value or somewhat nearing.

List<int> comb = new List<comb>() { 618, 350, 308, 300, 250, 232, 200, 128 };

The above list contains the integer values from which the proper combination has to be generated

The combination should have least number of values i.e., greatest number has to used most

Example:

If Input from User [Value = 2386]

Combination 1 = 618 + 350 + 308 + 300 + 250 + 232 + 200 + 128

Combination 2 = 618 + 618 + 618 + 300 + 232

I have used the below code, but have missed some logic.



XML
public static void Main(string[] args)
    {
        //subtotal list
        List<int> totals = new List<int>(new int[] { 618, 350, 308, 300, 250, 232, 200, 128 });

        // get matches
        List<int[]> results = KnapSack.MatchTotal(2682, totals);

        // print results
        foreach (var result in results)
        {
            Console.WriteLine(string.Join(",", result));
        }

        Console.WriteLine("Done.");
    }

internal static List<int[]> MatchTotal(int theTotal, List<int> subTotals)
    {
        List<int[]> results = new List<int[]>();
        while (subTotals.Contains(theTotal))
        {
            results.Add(new int[1] { theTotal });
            subTotals.Remove(theTotal);
        }

        if (subTotals.Count == 0)
            return results;

        subTotals.Sort();

        double mostNegativeNumber = subTotals[0];
        if (mostNegativeNumber > 0)
            mostNegativeNumber = 0;

        if (mostNegativeNumber == 0)
            subTotals.RemoveAll(d => d > theTotal);

        for (int choose = 0; choose <= subTotals.Count; choose++)
        {
            IEnumerable<IEnumerable<int>> combos = Combination.Combinations(subTotals.AsEnumerable(), choose);

            results.AddRange(from combo in combos where combo.Sum() == theTotal select combo.ToArray());
        }
        return results;
    }


public static class Combination
{
        public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int choose)
        {
            return choose == 0 ?
                new[] { new T[0] } :
                elements.SelectMany((element, i) =>
                    elements.Skip(i + 1).Combinations(choose - 1).Select(combo => (new[] { element }).Concat(combo)));
        }
}


Is there any other way other than the above to get combinations
AnswerRe: Combination Algorithm Pin
Simon_Whale28-Oct-13 1:53
Simon_Whale28-Oct-13 1:53 
GeneralRe: Combination Algorithm Pin
santosh_011428-Oct-13 2:12
santosh_011428-Oct-13 2:12 
AnswerRe: Combination Algorithm Pin
BillWoodruff28-Oct-13 7:12
professionalBillWoodruff28-Oct-13 7:12 
SuggestionRe: Combination Algorithm Pin
GuyThiebaut28-Oct-13 7:14
professionalGuyThiebaut28-Oct-13 7:14 
AnswerRe: Combination Algorithm Pin
harold aptroot28-Oct-13 22:56
harold aptroot28-Oct-13 22:56 
Question[C#] Infix to Postfix calculator Pin
Sottyoru27-Oct-13 5:14
Sottyoru27-Oct-13 5:14 
AnswerRe: [C#] Infix to Postfix calculator Pin
Richard MacCutchan27-Oct-13 6:15
mveRichard MacCutchan27-Oct-13 6:15 
AnswerRe: [C#] Infix to Postfix calculator Pin
codestar00727-Oct-13 23:03
professionalcodestar00727-Oct-13 23:03 
QuestionDll Files in .NET Pin
Amit Saini27-Oct-13 2:30
professionalAmit Saini27-Oct-13 2:30 
AnswerRe: Dll Files in .NET Pin
Abhinav S27-Oct-13 3:34
Abhinav S27-Oct-13 3:34 
GeneralRe: Dll Files in .NET Pin
harold aptroot27-Oct-13 7:21
harold aptroot27-Oct-13 7:21 
GeneralRe: Dll Files in .NET Pin
Mycroft Holmes27-Oct-13 14:19
professionalMycroft Holmes27-Oct-13 14:19 
GeneralRe: Dll Files in .NET Pin
harold aptroot27-Oct-13 20:49
harold aptroot27-Oct-13 20:49 
GeneralRe: Dll Files in .NET Pin
Amit Saini28-Oct-13 4:00
professionalAmit Saini28-Oct-13 4:00 
GeneralRe: Dll Files in .NET Pin
Pete O'Hanlon28-Oct-13 4:18
mvePete O'Hanlon28-Oct-13 4:18 
GeneralRe: Dll Files in .NET Pin
Dave Kreskowiak28-Oct-13 7:05
mveDave Kreskowiak28-Oct-13 7:05 
Questionsedna xml & exist-db sample code? Pin
Moustafa Farhat27-Oct-13 1:56
Moustafa Farhat27-Oct-13 1:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.